Home > Article > Web Front-end > How to create waterfall layout using pure CSS3? Brief analysis of columns method
In the previous article "How to add dynamic color-changing effects to background images in CSS3", we introduced the method of creating color-changing background image animations to make web pages look high-end! This time we will talk about how to use the CSS3 column series properties to implement waterfall flow layout. Friends who are interested can learn more~
When we mention CSS responsive layout, we will want to use Grid and Flexbox to implement it. , in fact they also have some limitations. Things like waterfall flow layout cannot be easily implemented using them.
The reason for this is that waterfall flows generally have the same width, but the height is adaptive according to the picture. And the position of the picture is also based on the position of the picture above.
So how to use pure CSS3 to implement waterfall flow layout? We can take advantage of the CSS3 column series properties!
Let’s start with the code directly:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body, html { position: relative; width: 100%; height: 100%; background: #4f000b; font-family: "PT Mono", monospace; } .masonry { -moz-column-count: 1; column-count: 1; /* 设置列数 */ -moz-column-gap: 0; column-gap: 0; /* 设置列间距 */ counter-reset: item-counter; } /* 根据不同的屏幕宽度 设置不同的列数*/ @media screen and (min-width: 400px) { .masonry { -moz-column-count: 2; column-count: 2; } } @media screen and (min-width: 600px) { .masonry { -moz-column-count: 3; column-count: 3; } } @media screen and (min-width: 800px) { .masonry { -moz-column-count: 4; column-count: 4; } } @media screen and (min-width: 1100px) { .masonry { -moz-column-count: 5; column-count: 5; } } .item { box-sizing: border-box; -moz-column-break-inside: avoid; break-inside: avoid; padding: 10px; counter-increment: item-counter; } .item__content { position: relative; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 220px; font-size: 40px; color: #360007; background: currentColor; box-sizing: border-box; color: #720026; } .item__content:hover { background: #9b0034; } .item__content:before { position: absolute; top: 0; left: 0; font-size: 13px; width: 2em; height: 2em; line-height: 2em; text-align: center; font-weight: bold; background-color: #222; content: counter(item-counter); } .item__content--small { color: #ce4257; height: 100px; } .item__content--small:hover { background: #d66274; } .item__content--medium { color: #ffc093; height: 175px; } .item__content--medium:hover { background: #ffd8bc; } .item__content--large { color: #ff7f51; height: 280px; } .item__content--large:hover { background: #ff9d7a; } </style> </head> <body> <div class="masonry"> <div class="item"> <div class="item__content"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> <div class="item"> <div class="item__content item__content--medium"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> <div class="item"> <div class="item__content item__content--medium"> </div> </div> <div class="item"> <div class="item__content"> </div> </div> <div class="item"> <div class="item__content item__content--large"> </div> </div> <div class="item"> <div class="item__content item__content--medium"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> <div class="item"> <div class="item__content"> </div> </div> <div class="item"> <div class="item__content item__content--large"> </div> </div> <div class="item"> <div class="item__content"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> <div class="item"> <div class="item__content item__content--large"> </div> </div> <div class="item"> <div class="item__content item__content--medium"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> <div class="item"> <div class="item__content item__content--medium"> </div> </div> <div class="item"> <div class="item__content"> </div> </div> <div class="item"> <div class="item__content item__content--small"> </div> </div> </div> </body> </html>
The effect is as shown below:
ok, The waterfall flow layout is implemented! So let’s analyze the above code and introduce you to several key css attributes:
@media
Query: Different styles can be set for different screen sizes
@media mediatype and|not|only (media feature) { CSS-Code; }
column-count
Attribute: Specifies the number of columns an element should be divided into.
column-gap
Property: Specifies the column gap.
column-gap: length|normal; length 一个指定的长度,将设置列之间的差距 normal 指定一个列之间的普通差距。 W3C建议1EM值
break-inside
Attribute: Describes how the content box under the multi-column layout page is broken if the multi-column layout has no content box, this attribute will be ignored.
In the above example:
.item { break-inside: avoid; box-sizing: border-box; padding: 10px; }
break-inside:avoid
In order to control the text block to be broken into separate columns to prevent the content of the item list from spanning columns, destroying the overall layout.
counter-increment
Attribute: Increment one or more counter values, usually used for counter-reset attribute and content attribute. For example, in the above example:
.item { counter-increment: item-counter; } .item__content:before { content: counter(item-counter); }
The PHP Chinese website platform has a lot of video teaching resources. Everyone is welcome to learn "css Video Tutorial"!
The above is the detailed content of How to create waterfall layout using pure CSS3? Brief analysis of columns method. For more information, please follow other related articles on the PHP Chinese website!