Skip to content
flex布局从左到右排列及换行保持间距的代码(模板)
html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Flexbox Layout</title>
  <style>
    .container {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      /* 从左到右排列 */
    }

    .item {
      /* 这里分成几列就用100%除以几,如我这里分三列就是100% / 3 得到33.33%的宽,也就是三个列的宽度刚好占满,
      如果需要加左右缝隙,如果左右间距10px,也就是要空出来20px间隙,则每列的宽要减去对应的间隙,然后加个margin:10px就可以了 */
      width: calc(33.33% - 20px);  
      /* 每行显示3个元素,考虑间距 */
      margin: 10px;
      /* 元素间隔 */
      background-color: lightblue;
      box-sizing: border-box;
      /* 让元素的border和padding计入宽度 */
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
    <div class="item">10</div>
    <div class="item">11</div>
  </div>
</body>

</html>