css如何实现多格布局
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍了css如何实现多格布局,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。多格布局多格布局指容器内节点以动态数量的格子形式
千家信息网最后更新 2026年11月01日css如何实现多格布局
这篇文章主要介绍了css如何实现多格布局,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
多格布局
多格布局指容器内节点以动态数量的格子形式排列的占位布局。微信朋友圈的相册就是最常见的多格布局了,当单张照片排列、两张照片排列、三张照片排列等等,每种情况下照片的尺寸都可能不一致。笔者制作了一个动态多格相册怀念我家狗狗AB。大家感受下纯CSS实现动态数量的多格布局吧。
在此留个悬念,不讲解如何实现,看看大家能不能根据笔者列出的提示尝试将该效果复原。主要原理是根据结构选择器限制节点范围实现,在本文也可找到原理的答案喔!记得实现完再看以下源码哈!

@mixin square($count: 2) { $length: calc((100% - #{$count} * 10px) / #{$count}); width: $length; height: $length;}.multigrid-layout { display: flex; flex-wrap: wrap; justify-content: flex-start; align-content: flex-start; padding: 5px; border: 1px solid #ccc; border-radius: 5px; width: 400px; height: 400px; li { display: flex; overflow: hidden; justify-content: center; margin: 5px; background-color: #f0f0f0; @include square(3); } img { width: 100%; height: 100%; object-fit: cover; }}// 一个元素.item:only-child { border-radius: 10px; width: auto; max-width: 80%; height: auto; max-height: 80%;}// 两个元素.item:first-child:nth-last-child(2),.item:first-child:nth-last-child(2) ~ .item:nth-child(2) { @include square(2);}.item:first-child:nth-last-child(2) { border-radius: 10px 0 0 10px;}.item:first-child:nth-last-child(2) ~ .item:nth-child(2) { border-radius: 0 10px 10px 0;}// 三个元素.item:first-child:nth-last-child(3),.item:first-child:nth-last-child(3) ~ .item:nth-child(2),.item:first-child:nth-last-child(3) ~ .item:nth-child(3) { @include square(2);}.item:first-child:nth-last-child(3) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(3) ~ .item:nth-child(2) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(3) ~ .item:nth-child(3) { border-bottom-left-radius: 10px;}// 四个元素.item:first-child:nth-last-child(4),.item:first-child:nth-last-child(4) ~ .item:nth-child(2),.item:first-child:nth-last-child(4) ~ .item:nth-child(3),.item:first-child:nth-last-child(4) ~ .item:nth-child(4) { @include square(2);}.item:first-child:nth-last-child(4) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(4) ~ .item:nth-child(2) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(4) ~ .item:nth-child(3) { border-bottom-left-radius: 10px;}.item:first-child:nth-last-child(4) ~ .item:nth-child(4) { border-bottom-right-radius: 10px;}// 五个元素.item:first-child:nth-last-child(5) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(5) ~ .item:nth-child(3) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(5) ~ .item:nth-child(4) { border-bottom-left-radius: 10px;}// 六个元素.item:first-child:nth-last-child(6) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(6) ~ .item:nth-child(3) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(6) ~ .item:nth-child(4) { border-bottom-left-radius: 10px;}.item:first-child:nth-last-child(6) ~ .item:nth-child(6) { border-bottom-right-radius: 10px;}// 七个元素.item:first-child:nth-last-child(7) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(7) ~ .item:nth-child(3) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(7) ~ .item:nth-child(7) { border-bottom-left-radius: 10px;}// 八个元素.item:first-child:nth-last-child(8) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(8) ~ .item:nth-child(3) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(8) ~ .item:nth-child(7) { border-bottom-left-radius: 10px;}// 九个元素.item:first-child:nth-last-child(9) { border-top-left-radius: 10px;}.item:first-child:nth-last-child(9) ~ .item:nth-child(3) { border-top-right-radius: 10px;}.item:first-child:nth-last-child(9) ~ .item:nth-child(7) { border-bottom-left-radius: 10px;}.item:first-child:nth-last-child(9) ~ .item:nth-child(9) { border-bottom-right-radius: 10px;}感谢你能够认真阅读完这篇文章,希望小编分享的"css如何实现多格布局"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
元素
布局
篇文章
动态
照片
原理
数量
朋友
相册
笔者
节点
一致
三个
两个
价值
兴趣
同时
容器
就是
尺寸
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
蓝思科技飞鸽互联网
互联网和科技技术
互联网是不是新科技
朗新科技与工业互联网
互联网 科技
互联网的科技创新
互联网科技logo设计
奔汉互联网科技
互联网科技论坛
互联网科技有限公司 翻译
隆华科技工业互联网
湖南一路畅行互联网科技
互联网科技商业
迪庆互联网科技招生
伊宁互联网科技做什么的
西安有哪些互联网高科技企业
互联网科技手机排行榜
银川新华互联网科技学校原画
杭州融税互联网科技有限公司
互联网领先科技成果宣传视频
互联网科技行业发展
智能科技公司是互联网公司吗
北京大学互联网科技怎么样
银川新华互联网科技职业技术培训学校图片
速维有品互联网信息科技(苏州)有限公司
晋城互联网科技
群盈汇互联网科技有限公司
小米互联网科技园入驻
上海互联网科技公司ppt
黑龙江互联网科技公司
