Arrange 2 items per row using flexbox

后端 未结 2 511
暗喜
暗喜 2020-12-23 13:17

Imagine I have following markup

<
相关标签:
2条回答
  • 2020-12-23 13:44

    you can give flex: 0 50% to children divs without touching .item

    .item {
      width: 100%
    }
    
    .container {
      display: flex;
      flex-wrap: wrap;
    }
    
    .container>div {
      flex: 0 50%;
      /*demo*/
      border: red solid;
      box-sizing:border-box
    }
    <div class="container">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
    </div>

    0 讨论(0)
  • 2020-12-23 13:56

    Assuming that the width property must not be changed, I can think of a few possible solutions, the first being fit-content which has amazingly poor browser support. The second, is use positioning, which is an equally bad idea due to its finicky nature and likelihood to fail on different screen sizes.

    Now the last two, are very similar, one is to use two containers, give them display:inline; give them a limited width, and fill them with items. If you didn't notice, this is essentially a 2×1 table.

    Lastly you could make a 2×1 table, and while it is probably the easiest solution here, it is a bad idea to get in the habit of using tables to position things other than forms and tables. However, it is an option that I will make available to you.

    Good luck!

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