Waterfall flow layout is a layout that was very popular before and is now more common.
This kind of uneven-looking multi-column layout focuses on each element, etc. The width is not equal to the height, which is commonly used in picture websites.
As the page scrolls, data blocks will be loaded continuously at the bottom of each column. In theory, it can be loaded infinitely without making the page beautiful.
Pinterest , Huaban.com are relatively mature websites that adopt waterfall flow layout
Rendering:
JavaScript + Absolute positioning
<div id="main"> ... <div class="box"> <div class="wrapper"> <div class="pic"><img src="" / alt="Two methods to implement waterfall flow layout" ></div> <div class="text">简单介绍</div> </div> </div> ...</div>
* {margin: 0; padding: 0;}html, body {width: 100%; height: 100%; background-color: #EDEDED;}#main { position: relative; width: 1280px; /*限定父容器的宽度*/ margin: 0 auto; }.box { position: absolute; padding: 7px; /*不建议使用 margin*/ box-sizing: border-box; width: 256px; /*限定数据块的宽度*/}.wrapper { padding: 5px; background-color: #fff; border: 1px solid #e4e4e4; box-shadow: 0 1px 5px rgba(0, 0, 0, .2); }.pic { font-size: 0; /*消除行内元素的间隙*/}.pic img { width: 100%; height: auto; }.text { color: #999; background: #FAFAFA; font-size: 14px; padding-top: 5px; }
window.onload = function() { waterFall('main', 'box') } function waterFall(main, box) { var main = document.getElementById(main); var boxs = main.getElementsByClassName(box); var boxWidth = boxs[0].offsetWidth; //获取每个数据块的宽度 var mainWidh = main.offsetWidth; //获取主容器的宽度 var cols = Math.floor(mainWidh / boxWidth); //计算列数 var heightArr = []; //定义一个存放 top 值的数组 for(var i = 0; i < boxs.length; i++) { var boxHeight = boxs[i].offsetHeight; if(i < cols) { //第一行 heightArr.push(boxs[i].offsetHeight); boxs[i].style.top = '0px'; boxs[i].style.left = i * boxWidth +'px'; } else { //求出最矮盒子的高度 var minBoxHeight = Math.min.apply(this, heightArr); //求出最矮盒子的索引 var minBoxIndex = getIndex(minBoxHeight, heightArr); boxs[i].style.top = minBoxHeight + 'px'; //顶部间距即最矮盒子的高度 boxs[i].style.left = minBoxIndex * boxWidth +'px'; //关键:更新最矮盒子的高度 heightArr[minBoxIndex] += boxHeight; } } } function getIndex(val, arr) { for(var i in arr) { if(val == arr[i]) { return i; } } }
#main { width: 1280px; margin: 0 auto; -webkit-column-count: 5; column-count: 5; -webkit-column-gap: 0; column-gap: 0; }.box { padding: 7px; box-sizing: border-box; width: 256px; }However, the waterfall flow achieved in this way is actually arranged vertically, while the previous classic solution was arranged horizontally. As a new attribute of CSS3, only IE10 and above browsers support it. columnI believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website! Related reading:
How to define the minimum height of the inline element span
How to use the html area image hotspot
What are the operations of borderless and borderless iframes
The above is the detailed content of Two methods to implement waterfall flow layout. For more information, please follow other related articles on the PHP Chinese website!

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor
