CSS3 flexbox layout max 3 child items on one line

后端 未结 6 1023
余生分开走
余生分开走 2021-02-07 03:05

Is their an simple way in CSS to have a fixed maximum of child items on the same line, before you push the next child elements to a new line?

6条回答
  •  梦谈多话
    2021-02-07 03:46

    fiddle

    #container {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
    #container>div {
      margin: 15px;
      width: 150px;
      height: 150px;
    }
    
    /* negative paddings are treated as 0 so woohoo */
    #container>div {
      /* up to 3 boxes in a row */
      padding: 0 calc((100% - 180px * 3) / 6);
    }
    #container>div {
      /* up to 4 boxes in a row */
      //padding: 0 calc((100% - 180px * 4) / 8);
    }
    #container>div {
      /* up to 5 boxes in a row */
      //padding: 0 calc((100% - 180px * 5) / 10);
    }
    /* 180px = width + horizontal margins */
    

提交回复
热议问题