html 瀑布流样式,纯css如何实现瀑布流?css实现瀑布流的两种方式

本文详细介绍了如何使用CSS的multi-column和Flex布局来创建瀑布流效果。通过column-count、column-gap和break-inside属性,实现了多列布局的瀑布流。同时,通过设置Flex布局,展示了另一种实现方式,特别强调了CSS在布局灵活性上的优势。虽然纯CSS瀑布流布局存在一定局限,但依然可以作为理解布局原理的良好实践。
摘要由CSDN通过智能技术生成

瀑布流的布局感觉还是蛮不错的,所以本篇文章就给大家来分享一下css实现瀑布流布局的两种方法,通过multi-column多列布局实现瀑布流和flex布局实现瀑布流。

1.multi-column多列布局实现瀑布流

先简单的讲下multi-column相关的部分属性column-count设置列数

column-gap设置列与列之间的间距

column-width设置每列的宽度

还要结合在子容器中设置break-inside防止多列布局,分页媒体和多区域上下文中的意外中断break-inside属性值

auto 指定既不强制也不禁止元素内的页/列中断。

avoid 指定避免元素内的分页符。

avoid-page 指定避免元素内的分页符。

avoid-column 指定避免元素内的列中断。

avoid-region 指定避免元素内的区域中断。截取了部分,可自己填充/* html文件 */

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

/* css样式 */

body {

background: #e5e5e5;

}

/* 瀑布流最外层 */

#root {

margin: 0 auto;

width: 1200px;

column-count: 5;

column-width: 240px;

column-gap: 20px;

}

/* 每一列图片包含层 */

.item {

margin-bottom: 10px;

/* 防止多列布局,分页媒体和多区域上下文中的意外中断 */

break-inside: avoid;

background: #fff;

}

.item:hover {

box-shadow: 2px 2px 2px rgba(0, 0, 0, .5);

}

/* 图片 */

.itemImg {

width: 100%;

vertical-align: middle;

}

/* 图片下的信息包含层 */

.userInfo {

padding: 5px 10px;

}

.avatar {

vertical-align: middle;

width: 30px;

height: 30px;

border-radius: 50%;

}

.username {

margin-left: 5px;

text-shadow: 2px 2px 2px rgba(0, 0, 0, .3);

}

80c9fc840d913d41a7f65279b61a6779.png

(瀑布流布局效果图1)

d1ca787ea67d7d93df40c9ca77265396.png

2.flex布局实现瀑布流将外层设置为row布局,然后再设置一个容器并设置为column布局,它是将列作为一个整体,然后在对列进行划分,在列里进行宽固定来实现的/* html文件(只截取两列布局)*/

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

牵起你的左手护着你

/* css文件 */

body{

background: #e5e5e5;

}

#root{

display: flex;

flex-direction: row;

margin: 0 auto;

width: 1200px;

}

.itemContainer{

margin-right: 10px;

flex-direction: column;

width: 240px;

}

.item{

margin-bottom: 10px;

background: #fff;

}

.itemImg{

width: 100%;

}

.userInfo {

padding: 5px 10px;

}

.avatar {

vertical-align: middle;

width: 30px;

height: 30px;

border-radius: 50%;

}

.username {

margin-left: 5px;

text-shadow: 2px 2px 2px rgba(0, 0, 0, .3);

}

0a692cbbd8237ce33c76e2b59c34225e.png

(瀑布流布局效果图2)

实践后发现,纯css实现的瀑布流只能是一列一列的排布,所以还是得用js来实现瀑布流布局更符合我们常见的瀑布流

相关推荐:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值