Flexbox: 4 items per row

前端 未结 9 1167
南笙
南笙 2020-11-22 13:01

I\'m using a flex box to display 8 items that will dynamically resize with my page. How do I force it to split the items into two rows? (4 per row)?

Here is a releva

9条回答
  •  轮回少年
    2020-11-22 13:19

    Here's another way without using calc().

    // 4 PER ROW
    // 100 divided by 4 is 25. Let's use 21% for width, and the remainder 4% for left & right margins...
    .child {
      margin: 0 2% 0 2%;
      width: 21%;
    }
    
    // 3 PER ROW
    // 100 divided by 3 is 33.3333... Let's use 30% for width, and remaining 3.3333% for sides (hint: 3.3333 / 2 = 1.66666)
    .child {
      margin: 0 1.66666% 0 1.66666%;
      width: 30%;
    }
    
    // and so on!
    

    That's all there is to it. You can get fancy with the dimensions to get a more aesthetic sizes but this is the idea.

提交回复
热议问题