Prevent next row taking up height of tallest item in previous row with flexbox?

后端 未结 1 1364
后悔当初
后悔当初 2020-12-22 04:23

I made a layout with flexbox consisting of 2 columns. When everything is the same height it works fine:

https://codepen.io/anon/pen/rJZeJm

&         


        
相关标签:
1条回答
  • 2020-12-22 04:41

    Flexbox can't do that by itself in a flexible way.

    As you mentioned yourself, CSS Grid can do this, though since you didn't want that, this suggestion is based on the assumption that you at narrower screens want the b to be at the bottom.

    By simply initially use float, and then at a certain break point, swap between float and display: flex, it is very easy to take advantage of both.

    Additionally, using Flexbox's order property, one can easily position the elements in any desired order, and with this avoid script and get a reasonable level of browser support.

    Updated codepen

    Stack snippet

    * {
      box-sizing: border-box;  
    }
    
    .cont {
      background: gold;
      overflow: hidden;
    }
    
    .a,
    .b,
    .c {
      width: 45%;
      padding: 10px;
      float: left;
    }
    
    .a {
      background: red;
    }
    
    .b {
      background: blue;
      color: white;
      float: right;
    }
    
    .c {
      background: orange;
    }
    
    @media (max-width: 500px) {
      .a,
      .b,
      .c {
        width: auto;
        float: none;
      }
      .b {
        order: 1;
      }
      .cont {
        display: flex;
        flex-direction: column;
      }
    }
    <div class="cont">
      <div class="a">
        A
      </div>
      <div class="b">
        B
        <br>
        More B
      </div>
      <div class="c">C</div>
    </div>

    0 讨论(0)
提交回复
热议问题