Home Web Front-end CSS Tutorial How to use CSS Positions layout to achieve waterfall flow effect on web pages

How to use CSS Positions layout to achieve waterfall flow effect on web pages

Sep 26, 2023 am 11:48 AM
responsive design waterfall layout css positioning (css positioning)

如何利用CSS Positions布局实现网页瀑布流效果

How to use CSS Positions layout to achieve the waterfall flow effect of web pages

Waterfall flow layout is a common web page layout method. It is characterized by elements arranged in an irregular manner. Arranged on the page, they flow from top to bottom like a waterfall. Waterfall flow layout is widely used in picture display, product display and other scenarios in web design. It can make good use of page space and display more content. In this article, we will introduce how to achieve the waterfall effect of web pages by using CSS Positions layout.

First, create a container containing multiple sub-elements in HTML to display the content of the waterfall flow layout. Each child element represents a single item or picture that will be displayed.

<div class="waterfall-container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
  ...
</div>
Copy after login

Next, you need to define CSS styles to implement waterfall layout. First, set the width and minimum height of the container, and use position: relative to position the child elements relative to the container.

.waterfall-container {
  width: 100%;
  min-height: 400px;
  position: relative;
}
Copy after login

Then, use position: absolute to position the child elements. We will use JavaScript to generate random heights for child elements so that we can achieve the waterfall effect. Here, we assume that there is already a JavaScript function named randomHeight() that can return a random number.

.item {
  width: 200px;
  position: absolute;
  padding: 10px;
  /* 元素水平间距和垂直间距 */
  margin: 10px;
  /* 定位子元素的初始位置 */
  top: 0;
  left: 0;
}
Copy after login

Next, we need to use JavaScript to calculate and apply the position of each child element. We will use two arrays columnHeights and columnIndexes to hold the height and index of each column.

var columnHeights = [0, 0, 0]; // 初始每列高度为0
var columnIndexes = [0, 1, 2]; // 初始每列索引为0, 1, 2
var columnCount = 3; // 列数
var itemWidth = 200; // 子元素宽度
var horizontalMargin = 20; // 水平间距
var verticalMargin = 20; // 垂直间距

var items = document.getElementsByClassName("item");
for (var i = 0; i < items.length; i++) {
  var shortestColumnHeight = Math.min.apply(null, columnHeights);
  var shortestColumnIndex = columnHeights.indexOf(shortestColumnHeight);

  items[i].style.top = columnHeights[shortestColumnIndex] + "px";
  items[i].style.left = shortestColumnIndex * (itemWidth + horizontalMargin) + "px";

  columnHeights[shortestColumnIndex] += items[i].offsetHeight + verticalMargin;
}
Copy after login

Through the above code, we can calculate and apply the position of sub-elements based on the height and index of each column, thereby achieving the effect of waterfall flow layout on the web page.

Finally, through the combination of CSS styles and JavaScript code, we can achieve the effect of waterfall flow layout on the web page. Of course, you can adjust the style and code according to actual needs to make it more consistent with your design requirements.

To summarize, the key to using CSS Positions layout to achieve the waterfall flow effect of web pages is to use position: absolute to position sub-elements, and use JavaScript to calculate and apply the position of each sub-element. With reasonable style settings and code calculations, we can easily implement a beautiful waterfall flow layout.

Reference:

  • W3Schools - CSS Positions: [https://www.w3schools.com/csS/css_positioning.asp](https://www.w3schools.com /csS/css_positioning.asp)
  • MDN Web Docs - position: [https://developer.mozilla.org/en-US/docs/Web/CSS/position](https://developer.mozilla .org/en-US/docs/Web/CSS/position)

The above is the detailed content of How to use CSS Positions layout to achieve waterfall flow effect on web pages. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles