css如何实现多格布局
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,这篇文章主要介绍了css如何实现多格布局,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。多格布局多格布局指容器内节点以动态数量的格子形式
千家信息网最后更新 2025年11月10日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安全错误
数据库的锁怎样保障安全
互联网公司算科技产业
软件开发招标是工程还是服务
南京软件开发就业情况
rmdb数据库
宝山区网络技术服务厂家价格
物集西游数据库修改
360网络安全中心招聘
数据库数据导出excel
焦作服务器价格
企业如何提升网络安全
关于阿里云服务器数据安全
数据库如何添加数据库
不能连入主机服务器
广东网络安全性
上海总齐网络技术有限公司
全民奇迹服务器
苏州蜂拥云网络技术有限公司
曙光服务器管理口账户
b75主板用服务器内存吗
连接数据库示例
江西数据库空投箱销售厂
信息安全服务器图片
三大数据库对比表
服务器有多大的
网络安全处理制度
移动实名制数据库多久更新
数据库主键 整型还是字符
北京互联网网络技术服务哪家好
提高网络安全知识
网络文件服务器
