在不使用js在情况下只用css实现瀑布流效果

使用到的是grid 布局,需要注意的是grid-template-rows: masonry; 目前只有Firefox 浏览器支持这个效果,而且还是一个实验性属性需要在设置里面开发实验性选项才行。

实例

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        #img {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-gap: 10px;
            grid-template-rows: masonry;
        }
        #img img {
            width: 200px;
        }
    </style>
</head>
<body>
    <div id="img">
    </div>
    <script>
        const img =  document.getElementById('img')
        let ele = ''
        for (let i = 0; i < 14; i++) {
            ele += `<img src="./images/img (${i+1}).jpg" />`
            
        }
        img.innerHTML = ele

    </script>
</body>
</html>
相关推荐
zbguolei15 分钟前
第002篇 · 战火中的电子黎明
开发语言·php
用户889561954817120 分钟前
戴上眼镜就把今天听完:绿光快报的 AIUI 语音新闻智能体
javascript
sean_wang1222 分钟前
Rust 轻量级虚拟机 smolvm 实战:200ms 冷启动、OCI 镜像直跑、单文件打包
开发语言·后端·rust·大鲸华章
stormsha30 分钟前
ResizeObserver loop completed with undelivered notifications:它只是个警告,但也是你代码的供词
javascript·css
战6171 小时前
Python 基础算法:三种排序、二分查找与递归
开发语言·python·算法
郝学胜-神的一滴2 小时前
量化交易从入门到精通 05:策略从哪来
开发语言·python·numpy·pandas·scikit-learn·matplotlib·scipy
Nazarick2 小时前
从 fetch 到 then:Demo在线订单系统 Ajax 回调的逐帧拆解
java·javascript·ajax·web
zbguolei2 小时前
第001篇 · 算盘与差分机
开发语言·php
ZHOUPUYU3 小时前
PHP 8.4 新特性实战指南:从属性钩子到异步编程
开发语言·php