V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
andyskaura
4.64D

关于 flex 布局的困惑

  •  
  •   andyskaura · May 30, 2024 · 2975 views
    This topic created in 709 days ago, the information mentioned may be changed or developed.

    现象是 flex 轴线上始终只能有一个元素,导致区域填不满,有没有什么好的方法指点一下。 要求 只用一个 flex 盒子,需要 item 的 order 做排序。

    在线编辑: https://codesandbox.io/p/sandbox/test-flex-9j8psr

    13 replies    2024-07-04 07:56:16 +08:00
    andyskaura
        1
    andyskaura  
    OP
       May 30, 2024
    liuhuihao
        2
    liuhuihao  
       May 30, 2024
    用 float ,别用 flex
    dongtingyue
        3
    dongtingyue  
       May 30, 2024
    1 和 2 是同一行。。。。
    EJW
        4
    EJW  
       May 30, 2024
    2 和 3 写在一个 div 里?
    iOCZS
        5
    iOCZS  
       May 30, 2024
    flex 不支持瀑布流
    Drool
        6
    Drool  
       May 30, 2024
    感觉 grid 更方便
    codehz
        7
    codehz  
       May 30, 2024
    首先 grid layout 也支持 order...
    Bologna
        8
    Bologna  
       May 30, 2024
    我正在做这个玩意,flex 无解,高度较小的 div 会独占那个空间,不能往下在塞东西,要么 grid ,要么 Vue Grid Layout
    codehz
        10
    codehz  
       May 30, 2024
    <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    </div>
    你这种布局用 grid 几秒搞定
    .container {
    display: grid;
    gap: 5px;
    grid-template-columns: 1fr 1fr;
    }

    .item:first-child {
    grid-row: 1 / span 2;
    }
    .item {
    box-shadow: 0 0 0 1px black;
    }

    order 也可以改顺序,不过这时候就不能用 first-child 来选择了,你需要自己在 order 为 1 的时候设置 grid row 的属性
    andyskaura
        11
    andyskaura  
    OP
       May 30, 2024
    @Drool
    @codehz
    @MRG0
    @codehz #10

    都忘了还有 grid 布局了,一直都是 flex 梭哈的,第一次遇到 flex 解决不了的情况。感谢各位哈,
    Al0rid4l
        12
    Al0rid4l  
       May 31, 2024
    flex 是一维的, 要么 float 要么 grid
    jaminq
        13
    jaminq  
       Jul 4, 2024 via iPhone
    flex 只有一根轴,grid 两根
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5468 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 08:59 · PVG 16:59 · LAX 01:59 · JFK 04:59
    ♥ Do have faith in what you're doing.