How to keep wrapped flex-items the same width as the elements on the previous row?

前端 未结 13 1149
一整个雨季
一整个雨季 2020-11-29 01:24

I have a

    that is a flex-box and a bunch of
  • s in it which are the flex-items.

    I am trying to get the

13条回答
  •  粉色の甜心
    2020-11-29 01:56

    I had the same issue, so I used a very tiny bit of Jquery to control the width of the boxes and make them all the same width even once they wrap.

    My demo is on CodePen Flex wrap, with equal width on new row

    function resize(){
      var colWidth;
      var screen = $(window).width(); 
      
      if(screen < 576){
        colWidth = Math.round(screen / 2) - 31;
        $(".item").width(colWidth);
      } 
      if(screen > 575 && screen < 768){
        colWidth = Math.round(screen / 3) - 31;
        $(".item").width(colWidth);
      } 
      else if (screen > 767 && screen < 992){
          colWidth = Math.round(screen / 4) -31;
          $(".item").width(colWidth);
      }
      else if (screen > 991 ){
          colWidth = Math.round(screen / 6) -31;
          $(".item").width(colWidth);
      }
    }
      
    window.onresize = function() {
      resize();
    }
    
    resize();
    html,
    body{
      padding:0;
      margin:0;
    }
    .flex-container {
      padding: 0;
      margin: 0;
      list-style: none;
      -ms-box-orient: horizontal;
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -moz-flex;
      display: -webkit-flex;
      display: flex;
      justify-content: left;
      background-color:#ffffd;
    }
    
    .wrap    { 
      -webkit-flex-wrap: wrap;
      flex-wrap: wrap;
    }  
    
    .item {
      background: tomato;
      height: 100px;
      margin:0 15px;
      line-height: 100px;
      color: white;
      font-weight: bold;
      font-size: 2em;
      text-align: center;
      margin-top:10px;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

提交回复
热议问题