flex 布局使用 space-between 时将最后一行左对齐

1.使用占位元素

特点:适用于任意列数布局,比较简单,缺点是会产生空标签

方法:使用循环体循环一整行空元素。宽度为单个元素宽度,高度为0

html 复制代码
<div class="flex-box">
  <div class="item-box" v-for="item in 4" :key="item"></div>
  <!-- 假设一行有3列,即3个元素 -->
  <div v-for="temp in 3" :key="temp + 't'" class="temp-box"></div>
</div>
css 复制代码
.flex-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex-box .item-box {
  width: 30%;
  height: 200px;
  margin-bottom: 20px;
  background: pink;
}
.flex-box .temp-box {
  width: 30%;
  height: 0;
  margin: 0;
}

2.给父级元素后面添加伪元素

特点:只针对三列布局

方法:通过 ::after 选择器,给父元素添加伪元素

html 复制代码
<div class="flex-box">
  <div class="item-box" v-for="item in 5" :key="item"></div>
</div>
css 复制代码
.flex-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex-box .item-box {
  width: 30%;
  height: 200px;
  margin-bottom: 20px;
  background: pink;
}
.flex-box:after {
  content: '';
  width: 30%;
}

3.计算方式

特点:适用于每一行列数固定,且列宽度固定,需要进行计算,相比较复杂

方法:对于最后一行的元素动态设置 margin-right。判断如果最后一个元素处于当前列,会发生布局错乱,则设置元素的 margin-right 为剩余空间的大小(剩余列宽度 + 剩余间隙大小)。假设元素宽度是 width,总间隙是 space(盒子宽度 - 元素宽度 * 列数)

计算公式:margin-right: calc( (space / 间隙数) \* 剩余列数 + width * 剩余列数)

三列布局

计算公式:宽度为30%,则剩余间隙:100% - 30% * 3 = 10%

html 复制代码
<div class="flex-box-3">
  <div class="item-box" v-for="item in 5" :key="item"></div>
</div>
css 复制代码
/* 计算方式(三列布局) */
.flex-box-3 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex-box-3 .item-box {
  width: 30%;
  height: 200px;
  margin-bottom: 20px;
  background: pink;
}
/* 三列布局:宽度为30%,则剩余间隙:100% - 30% * 3 = 10% */
/* 最后一行是2个元素 */
.flex-box-3 .item-box:last-child:nth-child(3n - 1) {
  margin-right: calc(10% / 2 * 1 + 30% * 1);
}

四列布局

计算公式:宽度为22%,则剩余间隙:100% - 22% * 4 = 12%

html 复制代码
<div class="flex-box-4">
  <div class="item-box" v-for="item in 5" :key="item"></div>
</div>
css 复制代码
/* 计算方式(四列布局) */
.flex-box-4 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex-box-4 .item-box {
  width: 22%;
  height: 200px;
  margin-bottom: 20px;
  background: pink;
}
/* 四列布局:宽度为22%,则剩余间隙:100% - 22% * 4 = 12% */
/* 最后一行是2个元素 */
.flex-box-4 .item-box:last-child:nth-child(4n - 2) {
  margin-right: calc(12% / 3 * 2 + 22% * 2);
}
/* 最后一行是3个元素 */
.flex-box-4 .item-box:last-child:nth-child(4n - 1) {
  margin-right: calc(12% / 3 * 1 + 22% * 1);
}

五列布局

计算公式:宽度为18%,则剩余间隙:100% - 18% * 5 = 10%

html 复制代码
<div class="flex-box-5">
  <div class="item-box" v-for="item in 7" :key="item"></div>
</div>
css 复制代码
/* 计算方式(五列布局) */
.flex-box-5 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.flex-box-5 .item-box {
  width: 18%;
  height: 200px;
  margin-bottom: 20px;
  background: pink;
}
/* 五列布局:宽度为18%,则剩余间隙:100% - 18% * 5 = 10% */
/* 最后一行是2个元素 */
.flex-box-5 .item-box:last-child:nth-child(5n - 3) {
  margin-right: calc(10% / 4 * 3 + 18% * 3);
}
/* 最后一行是3个元素 */
.flex-box-5 .item-box:last-child:nth-child(5n - 2) {
  margin-right: calc(10% / 4 * 2 + 18% * 2);
}
/* 最后一行是4个元素 */
.flex-box-5 .item-box:last-child:nth-child(5n - 1) {
  margin-right: calc(10% / 4 * 1 + 18% * 1);
}

依次类推,当列数为6、7、8、9... 时,同样按照上述方式计算

相关推荐
可乐加.糖23 分钟前
项目版本管理和Git分支管理方案
java·git·目标跟踪·gitlab·敏捷流程·源代码管理
wowocpp42 分钟前
spring boot Controller 和 RestController 的区别
java·spring boot·后端
waterHBO1 小时前
直接从图片生成 html
前端·javascript·html
EndingCoder1 小时前
JavaScript 时间转换:从 HH:mm:ss 到十进制小时及反向转换
javascript
互联网搬砖老肖1 小时前
React组件(一):生命周期
前端·javascript·react.js
我科绝伦(Huanhuan Zhou)2 小时前
深入解析Shell脚本编程:从基础到实战的全面指南
前端·chrome
小马哥编程2 小时前
React和Vue在前端开发中, 通常选择哪一个
前端·vue.js·react.js
aklry2 小时前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi2 小时前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
遗憾皆是温柔2 小时前
MyBatis—动态 SQL
java·数据库·ide·sql·mybatis