CSS怎么实现瀑布流?两种方式介绍
时间:2021-10-12 来源:互联网
今天PHP爱好者为您带来CSS怎么实现瀑布流?下面本篇文章给大家介绍一下使用CSS实现瀑布流的两种方式,希望对大家有所帮助!希望对大家有所帮助。
column-count 属性,是控制屏幕分为多少列。
column-gap 属性,是控制列与列之间的距离。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>瀑布流布局-column</title>
<style>
.box {
margin: 10px;
column-count: 3;
column-gap: 10px;
}
.item {
margin-bottom: 10px;
}
.item img{
width: 100%;
}
</style>
</head>
<body>
<p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
</p>
</body>
</html>

flex 弹性布局实现瀑布流
flex实现瀑布流需要将最外层元素设置为display: flex,使用弹性布局
flex-flow:column wrap使其纵向排列并且换行换行设置
height: 100vh填充屏幕的高度,也可以设置为单位为px的高度,来容纳子元素。每一列的宽度可用
calc函数来设置,即width: calc(100%/3 - 20px)。分成等宽的3列减掉左右两遍的margin距离。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>瀑布流布局-flex</title>
<style>
.box {
display: flex;
flex-flow: column wrap;
height: 100vh;
}
.item {
margin: 10px;
width: calc(100%/3 - 20px);
}
.item img{
width: 100%;
}
</style>
</head>
<body>
<p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
<p>
<img src="./imgs/2.jpg" alt="2" />
</p>
<p>
<img src="./imgs/3.jpg" alt="3" />
</p>
<p>
<img src="./imgs/1.jpg" alt="1" />
</p>
</p>
</body>
</html>
展示效果如下

案例代码
瀑布流实现代码:https://gitee.com/yunxii/css-demo/tree/master/waterfall
原文地址:https://juejin.cn/post/7011333433318178846
作者:tangxd3
以上就是CSS怎么实现瀑布流?两种方式介绍的详细内容,更多请关注php爱好者其它相关文章!
-
LOL手游传奇开启-Faker与TheShy联名皮肤将登场 2025-11-06 -
如鸢代号鸢决战常山吕布队-一星吕布庞羲可打 2025-11-06 -
燕云十六声猫之行活动本周回归-全新剑武器外观登场 2025-11-06 -
宝可梦大集结改名卡怎么获得-宝可梦训练家更名卡在哪 2025-11-06 -
2025年十大热门币交易所推荐:ETH、SOL、ARB交易首选平台 2025-11-06 -
永劫手游S9赛季预下载开启-参与预下载可获下载福利 2025-11-06