flex布局一行三列,最后一行不足自动向前排实现

 

 

一行三列循环自适应,是移动端常见的布局

当使用display flex justify-content: space-between;最后一行只有两个时候,会出现分居两端,我们此时需要把最后一行改为向前布局

我的解决方案如下:

<div>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item">1</p>
  <p class="item"></p> // 在最后一项添加一个空块
</div>

然后css

 

        .field-item:nth-last-of-type(1):nth-of-type(3n+1) {
          display: none;
        }

  如果有更好的,也欢迎给出

posted @ 2021-06-24 12:34  夜皇帝  阅读(1471)  评论(0编辑  收藏  举报