Home > Web Front-end > CSS Tutorial > How Can I Create a Masonry Layout Using Only CSS?

How Can I Create a Masonry Layout Using Only CSS?

DDD
Release: 2024-12-07 04:45:16
Original
135 people have browsed it

How Can I Create a Masonry Layout Using Only CSS?

Creating a Masonry-Style Layout Using CSS

Achieving a masonry-style layout, where elements have varying heights and widths, solely through CSS has long been a challenge. With the advent of CSS3, this challenge has been overcome.

CSS3 Solution

CSS3 introduces the column-count property, which allows you to specify the number of columns in which content should be laid out. By combining this property with the column-gap property, which defines the spacing between columns, it is possible to create a masonry-style layout.

.container {
  column-count: 2;
  column-gap: 10px;
  width: 360px;
}

.container div {
  display: inline-block;
  width: 100%;
  background-color: red;
}
Copy after login

In this code, the .container element defines two columns with a gap of 10px between them. The .container div elements are laid out within these columns, with each element spanning the full width of the column.

Non-CSS3 Solution

Unfortunately, for browsers that do not support CSS3, a masonry-style layout cannot be achieved solely through CSS. In such cases, you may need to resort to JavaScript libraries for this purpose.

The above is the detailed content of How Can I Create a Masonry Layout Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template