温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS中怎么实现多行多列布局

发布时间:2021-07-09 16:53:41 来源:亿速云 阅读:334 作者:Leah 栏目:web开发

本篇文章为大家展示了CSS中怎么实现多行多列布局,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

方案一:标签补位
我们都知道,之所以对不齐是因为最后一行的子项目没有达到4个,space-between的对齐方式,自然会把中间空出来。既然如此,何不直接补位,让元素排满4个。

<!-- 样式 --> <style>     .list{         display: flex;         justify-content: space-between;         flex-wrap: wrap;     }     .item{         /* flex: 0 0 24% 该属性等价于 flex:none;width:24% */         flex: 0 0 24%;         height: 100px;         background-color: aqua;         margin-bottom: 10px;     }     .item-fill{         flex: 0 0 24%;         height:0;     }     /* 消除最后一行多余边距 */     .list .item:nth-last-child(-n+4){         margin-bottom: 0;     } </style>  <!-- html --> <div class="list">     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>      <div class="item-fill"></div>     <div class="item-fill"></div>     <div class="item-fill"></div> </div>

效果如下:

CSS中怎么实现多行多列布局

如果子元素个数不是4个怎么办?我们细心观察,不难发现,最后一行的最小值是1个,那么我们只需要补位n-1个即可。如果只有3个,也可以用伪元素::after去补最后一个位置。

方案二:计算剩余空间
如果我们知道最后一行剩余空间的话,完全可以控制最后一个元素的边距或者缩放比例去占满剩下的空间,自然就能左对齐了。要做到这一点,首先得确定宽度和边距,宽度通常是已知的,我们只需要把边距确定下来,就能确认剩余空间。

接上面的的例子,假设一行有4个, 每个占比24%,4个就是24% * 4 = 96% , 那么可以确定总边距是4%,由于一行有4个元素,而最后一个的右边距是多余的,那么可以确定单个的边距为 4% / 3 = 1.333% , 计算出来后就可以开始写代码了:

<!-- css --> <style>     .list{         display: flex;         justify-content: space-between;         flex-wrap: wrap;     }     .item{         flex: 0 0 24%;         height: 100px;         background-color: aqua;         /* 边距懒得算,css函数代替 */         margin-right: calc(4% / 3);         margin-bottom: calc(4% / 3);     }     /* 去除每行尾的多余边距 */     .item:nth-child(4n){         margin-right: 0;     }     /* 使最后一个元素的边距填满剩余空间 */     .item:last-child{         margin-right: auto;     }     /* 也可以给列表增加一个占位元素,自动缩放填满剩余空间 */     /* .list::after{         content: '';         flex: 1;         height: 0;     } */     .list .item:nth-last-child(-n+4){         margin-bottom: 0;     } </style> <!-- html --> <div class="list">     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div> </div>

效果如下:

CSS中怎么实现多行多列布局

可能有些小伙伴觉得懒得记,那么下面直接给出封装好的sass mixin, 复制即可使用:

/**  * 多列布局  * $count 项目数量  * $itemWidth 项目宽度,百分比,不含百分号  * $itemHeight 项目高度,随意  */ @mixin grid($count:4, $itemWidth:20, $itemHeight:auto) {     $rest: 100 - $itemWidth * $count; // 剩余空间     $space: percentage($rest/($count - 1)/100); // 边距     display: flex;     flex-wrap: wrap;      /*此处的*号建议替换成具体的布局容器(div,view...),以加快css解析*/     & > * {         flex: 0 0 #{$itemWidth + '%'};         height: $itemHeight;         margin-right: $space;         margin-bottom: $space;         box-sizing: border-box;          &:nth-child(#{$count}n) {             margin-right: 0;         }          &:nth-last-child(-n + #{$count}) {             margin-bottom: 0;         }          /*为了兼容space-between的布局,占满剩余空间*/         &:last-child {             margin-right: auto;         }     } }  /*使用方法*/ .list{     /* 一行4项,每项20%宽度 */     @include grid(4,20) }

以上为flex版本,假如你需要兼容ie浏览器,那么可以用float布局替换,float自动左对齐,也就不需要填充最后的剩余空间了。

方案三:网格布局
网格布局,默认就是左对齐,即使使用space-between。

<style>     .list {         display: grid;         justify-content: space-between;         grid-template-columns: 1fr 1fr 1fr 1fr; /*设置等比列*/         gap: 10px; /*行间距*/     }     .item{         background-color: aqua;         height: 100px;     } </style>  <!-- html --> <div class="list">     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div> </div>

效果如下:

CSS中怎么实现多行多列布局

上面的三个方案各有各的好处:


  1. 方案一的缺点是实现不够优雅,需要增加无用的占位标签。

  2. 方案二倒没什么缺点,除了写法会复杂点,但只要封装好mixin在sass中使用足够简单,即使需要兼容ie,也只需要换成float即可。

  3. 方案三,兼容性最差,无法在ie中正常使用,但用法最简单,布局甚至比flex还要强大。

上述内容就是CSS中怎么实现多行多列布局,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI