

Masonry Layout - CSS: Cascading Style Sheets | MDN
source link: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Masonry_Layout
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Masonry Layout
Level 3 of the CSS Grid Layout specification includes a masonry
value for grid-template-columns
and grid-template-rows
. This guide details what masonry layout is, and how to use it.
Important: This feature is only implemented in Firefox, and can be enabled by setting the flag layout.css.grid-template-masonry-value.enabled
to true
, in order to allow testing and providing of feedback.
Masonry layout is a layout method where one axis uses a typical strict grid layout, of most often columns, and the other a masonry layout. On the masonry axis, rather than sticking to a strict grid with gaps being left after shorter items, the items in the following row rise up to completely fill the gaps.
Creating a masonry layout
To create the most common masonry layout, your columns will be the grid axis and the rows the masonry axis. Define this layout with grid-template-columns
and grid-template-rows
:
.container {
display: grid;
gap: 10px;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
grid-template-rows: masonry;
}
The child elements of this container will now lay out item by item along the rows, as they would with regular grid layout autoplacement. However, as they move onto a new row the items will display according the masonry algorithm. Items will load into the column with the most room causing a tightly packed layout without strict row tracks.
It is also possible to create a masonry layout with items loading into rows.
Controlling the grid axis
On the grid axis, things will work just as you expect them to in grid layout. You can cause items to span multiple tracks while remaining in auto-placement, using the span
keyword. Items may also be positioned using line-based positioning.
Masonry layout with spanning items
In this example two of the items span two tracks, and the masonry items work around them.
This example includes an item which has positioning for columns. Items with definite placement are placed before the masonry layout happens.
Controlling the masonry axis
The masonry axis operates under different rules as it is following the masonry layout rules rather than normal grid auto-placement rules. To control this axis we have three additional properties defined in the Grid Level 3 specification align-tracks
, justify-tracks
, and masonry-auto-flow
.
masonry-auto-flow
The masonry-auto-flow
property gives you a way to change how the masonry algorithm behaves. Give it a value of next
and items will display in order on the grid axis, rather than going into whichever track has the most free space. The value positioned
will ignore items with definite placement and place items in order-modified document order.
align-tracks
The align-tracks
property allows for the alignment of items in grid containers with masonry in their block axis. The property aligns the items within their track, much in the way flex layout works. The property takes the same values as align-content
, however you can specify multiple values to have different alignment values per track on the grid axis.
If you specify more values than tracks the additional values are ignored. If there are more tracks than values any additional tracks will use the last specified value.
justify-tracks
The justify-tracks
property works in the same way as align-tracks, however it is used when the masonry axis is the inline axis.
Fallback
In browsers that do not support masonry, regular grid auto-placement will be used instead.
See also
Recommend
-
105
Cascading Style Alignment Chart Add to FavoritesShareCascading S...
-
29
Overview This is a proposal to extend CSS Grid to support masonry layout in one of the axes while doing normal grid layout in the other. I'll use grid-template-rows/columns: masonry to specify the...
-
5
Using CSS animations CSS animations make it possible to animate transitions from one CSS style configuration to another. Animations consist of two components, a style describing the CSS animation and a set of k...
-
8
@dolamu-asipaDolamu AsipaHey there! i'm an aspiring developerBrief BackgroundCSS (Cascading Style Sheets) is not a...
-
4
CSS (Cascading Style Sheet)Hi everyone, it’s me again, Beauty James. In my previous articles, I discussed frontend and its languages. So today, I’ll be talking about CSS, another Frontend language. As we move further, we will discuss the foll...
-
13
Masonry Layout with Tailwind CSS Columns
-
7
A Use Case for Cascading Attribute Sheets October 20, 2022 It’s been some years since Tab Atkins-Bittner first pr...
-
7
mask-modeThe mask-mode CSS property sets whether the mask reference defined by
-
11
-
3
Native CSS masonry layoutPublished: 2023.02.09 · 2 minutes readA masonry type of layout, one of the biggest obsessions of UX designers, is finally coming to CSS. Style popularized by Pinterest, where elements fill the...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK