flex弹性布局;justify-content:space-between;最后一行左对齐

第一种方式:

需要在父级元素使用一个伪类,设置flex:1;使伪类自动填充剩余的空间;

这种方式会使最后一行的边距失效

:after {
    content: "";
    flex: 1;
}

第二种方式:

使用margin-right计算;这种方式适合每一行固定列数的情况

假设每一行只有3列元素,那么当最后一个元素是第二列(即.item:last-child:nth-child(3n + 2))的情况,才需要进行 margin-right处理,距离是一个元素的宽度+空隙宽度。

假设元素宽度是$width,上述情况所需要的距离:(100% - 3 * $width) / 2 + $width => (100% - $width) / 2;

使用 calc() 函数来计算时,通过 #{} 语法将 SCSS 变量转换为 CSS 变量

.item:last-child:nth-child(3n + 2) {
  margin-right: calc((100% - #{$width}) / 2);
}

同理,一行4列的情况,需要处理两种情况,最后一个元素在第二列 和 最后一个元素在第三列的情况。

.item:last-child:nth-child(4n + 2) {
  margin-right: calc((100% - #{$width}) / 3 * 2);
}
.item:last-child:nth-child(4n + 3) {
  margin-right: calc((100% - #{$width}) / 3 * 1);
}

第三种方式:

使用grid,适合一行列数不固定的情况

.list {
    display: grid;
    grid-template-columns: repeat(auto-fill, /*item的宽度*/ 100px);
    grid-gap: 10px;
}
.list .item {
    width: /*item的宽度*/ 100px;
}

参考文章:css3 flex布局 justify-content:space-between 最后一行左对齐_css3_CSS_网页制作_脚本之家

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值