码迷,mamicode.com
首页 > Web开发 > 详细

CSS 网格实现瀑布流

时间:2020-07-24 21:34:08      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:形式   count   content   内容   ini   initial   oid   lan   列表   

瀑布流布局其核心是基于一个网格的布局,而且每行包含的项目列表高度是随机的(随着自己内容动态变化高度),同时每个项目列表呈堆栈形式排列,最为关键的是,堆栈之间彼此之间没有多余的间距差存大。

技术图片

 

 

代码比较简单:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
        }
        .pu {
            margin: 0 auto;
            column-count: 5; /*分为5列 自己设置*/
            column-width: 300px; /*设置列宽,即是每列的宽度*/
            column-gap: 10px; /*每列和每行的间距为10px 是 column-gap: 10px; row-gap: 10px; 的缩写*/
        }
        .inner {
            break-inside: avoid; /*避免分隔符分开图片*/
            margin-bottom: 10px;   
            }
            img {
                width: 100%;
            }
    </style>
</head>
<body>
    <div class="pu">
        <p class="inner"> <img src="1.webp" > </p>
        <p class="inner"> <img src="1.webp" > </p>
        <p class="inner"> <img src="1.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="1.webp" > </p>
        <p class="inner"> <img src="1.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="2.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="3.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="4.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="6.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="11.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
        <p class="inner"> <img src="12.webp" > </p>
    </div>
</body>
</html>

当然也可以用flex 布局,不过使用flex会比较麻烦。

 

CSS 网格实现瀑布流

标签:形式   count   content   内容   ini   initial   oid   lan   列表   

原文地址:https://www.cnblogs.com/ximenchuifa/p/13374226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!