CSS masonry Property
The CSS masonry property was an experimental shorthand proposed for configuring masonry layouts with staggered rows or columns.
Masonry layouts arrange items of different sizes without requiring every item to align to a strict grid in both directions. The standalone masonry shorthand appeared in earlier proposals, but current CSS Grid Level 3 development uses a grid-lanes layout model. The older shorthand should not be relied upon for production websites.
masonry Syntax
The following syntax illustrates the historical standalone masonry proposal. It is included for reference and is not recommended for production use.
Historical Shorthand
.container {
display: masonry;
masonry: repeat(3, 1fr);
}
In the earlier proposal, this described a masonry container with three equal-width tracks.
Historical Row Direction
.container {
display: masonry;
masonry: row;
}
This earlier syntax proposed horizontal stacking instead of the usual vertical stacking.
Current Draft Approach
.container {
display: grid-lanes;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
The grid-lanes approach uses a dedicated display type and familiar grid track sizing. It remains experimental, so browser support must be checked.
Property Status
The standalone masonry property belongs to an earlier proposal for CSS masonry layout. It should be distinguished from the masonry layout concept itself, which continues to be developed.
| Characteristic | Status |
|---|---|
| Property name | masonry |
| Purpose | Historical shorthand for masonry track configuration |
| Specification status | Superseded experimental proposal |
| Standardized initial value | Not established for the historical shorthand |
| Inheritance | Not applicable as a current standardized property |
| Animation type | Not established |
| Current draft model | CSS Grid Level 3 grid-lanes layout |
| Production recommendation | Use supported layout methods with progressive enhancement |
Because the earlier shorthand was not finalized, it does not have a reliable standardized set of property values or computed-value rules.
How Masonry Layout Works
A masonry layout places items of different heights or widths into tracks while reducing the empty space that occurs in a conventional grid.
Traditional Grid Layout
In a typical row-based CSS Grid layout, items in the same row share a row track. A tall item can cause unused space below shorter items.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
Masonry Layout
In a vertical masonry layout, items are distributed among columns and stack according to the available space. Shorter items do not force all neighboring columns to leave matching gaps.
Typical Uses
- Image galleries containing photographs with different aspect ratios.
- Portfolio layouts with projects of varying heights.
- Card collections with different amounts of text.
- Editorial layouts containing mixed-size content blocks.
Masonry changes item placement, but it does not automatically change the content or dimensions of individual cards.
Historical Syntax
Earlier proposals explored a standalone display: masonry layout type with properties for track sizing, direction, and item placement.
Three Equal Columns
.gallery {
display: masonry;
masonry: repeat(3, 1fr);
gap: 16px;
}
Custom Track Sizes
.gallery {
display: masonry;
masonry: repeat(3, 200px);
gap: 16px;
}
Longhand Proposal
.gallery {
display: masonry;
masonry-template-tracks: repeat(3, 1fr);
gap: 16px;
}
These examples describe historical experimental syntax. They are not interchangeable with the current grid-lanes draft and may be ignored by browsers.
Grid Lanes Layout
The current CSS Grid Level 3 draft describes masonry-style placement using the grid-lanes display value.
Vertical Masonry
.gallery {
display: grid-lanes;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
The columns form the regular grid axis, while items stack along the other axis.
Responsive Columns
.gallery {
display: grid-lanes;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 16px;
}
This draft syntax defines columns that adapt to the available container width.
Horizontal Masonry
.gallery {
display: grid-lanes;
grid-template-rows: repeat(3, 120px);
gap: 16px;
}
Defining row tracks instead of column tracks provides the basis for a horizontally stacking masonry layout.
Progressive Enhancement
.gallery {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
@supports (display: grid-lanes) {
.gallery {
display: grid-lanes;
}
}
Browsers without grid-lanes support retain the regular grid layout. Feature detection checks whether the browser recognizes the declaration, not whether every experimental behavior works correctly.
CSS Grid Fallback
CSS Grid is a reliable alternative when strict rows are acceptable. It provides responsive columns, consistent spacing, and broad browser support.
Responsive Card Grid
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}
.gallery-item {
padding: 20px;
background-color: #e2e8f0;
border-radius: 6px;
}
This approach does not produce true masonry stacking. Items still participate in regular grid rows, so taller cards can leave gaps below shorter cards.
When to Use CSS Grid
Choose CSS Grid when predictable source order, aligned rows, and stable cross-browser behavior matter more than eliminating every vertical gap.
CSS Columns Alternative
CSS Multi-column Layout can create a masonry-like visual arrangement using supported CSS properties.
Column-Based Layout
.gallery {
columns: 3;
column-gap: 16px;
}
.gallery-item {
break-inside: avoid;
margin-bottom: 16px;
padding: 20px;
background-color: #e2e8f0;
}
Items flow down one column before continuing into the next, rather than filling across rows. This distinction is important for reading order and keyboard navigation.
Responsive Columns
.gallery {
columns: 3;
column-gap: 16px;
}
@media (max-width: 767px) {
.gallery {
columns: 2;
}
}
@media (max-width: 479px) {
.gallery {
columns: 1;
}
}
CSS columns provide a useful fallback for decorative galleries where column-first reading order is acceptable.
Masonry Example
This example uses CSS columns to create a masonry-style card gallery that works in current browsers. It does not depend on the experimental masonry property.
HTML Example
<div class="masonry-demo">
<div class="masonry-card">
<h3>HTML Elements</h3>
<p>Learn the structure of a web page.</p>
</div>
<div class="masonry-card">
<h3>CSS Selectors</h3>
<p>Target elements using classes, IDs, attributes, and relationships.</p>
<p>Combine selectors to create flexible styling rules.</p>
</div>
<div class="masonry-card">
<h3>Flexbox</h3>
<p>Arrange items along a row or column.</p>
</div>
<div class="masonry-card">
<h3>CSS Grid</h3>
<p>Create layouts using rows and columns.</p>
<p>Control alignment, sizing, and spacing.</p>
</div>
<div class="masonry-card">
<h3>Responsive Design</h3>
<p>Adapt layouts to different screen sizes.</p>
</div>
<div class="masonry-card">
<h3>Accessibility</h3>
<p>Build interfaces that are easier to use.</p>
<p>Use semantic HTML and clear navigation.</p>
</div>
</div>
CSS Example
.masonry-demo {
columns: 3;
column-gap: 16px;
padding: 16px;
background-color: #f1f5f9;
}
.masonry-card {
break-inside: avoid;
margin-bottom: 16px;
padding: 16px;
background-color: #fff;
border: 1px solid #cbd5e1;
border-radius: 6px;
}
.masonry-card h3 {
margin: 0 0 10px;
font-size: 1rem;
}
.masonry-card p {
margin: 0 0 10px;
}
.masonry-card p:last-child {
margin-bottom: 0;
}
@media (max-width: 767px) {
.masonry-demo {
columns: 2;
}
}
@media (max-width: 479px) {
.masonry-demo {
columns: 1;
}
}
HTML Elements
Learn the structure of a web page.
CSS Selectors
Target elements using classes, IDs, attributes, and relationships.
Combine selectors to create flexible styling rules.
Flexbox
Arrange items along a row or column.
CSS Grid
Create layouts using rows and columns.
Control alignment, sizing, and spacing.
Responsive Design
Adapt layouts to different screen sizes.
Accessibility
Build interfaces that are easier to use.
Use semantic HTML and clear navigation.
The cards have different heights and flow vertically through the columns. The layout adjusts to two columns on smaller screens and one column on narrow screens.
This is a supported masonry-style alternative rather than a demonstration of the experimental masonry shorthand.
Common Mistakes
Treating masonry as a Standard Property
.gallery {
masonry: repeat(3, 1fr);
}
This historical shorthand is not a standardized, interoperable property. Browsers may ignore it.
Assuming display: masonry Is Widely Supported
.gallery {
display: masonry;
}
This syntax appeared in earlier experimental implementations. It should not be used without understanding the specific browser implementation being tested.
Confusing CSS Grid with Masonry
Ordinary CSS Grid aligns items into row and column tracks. It does not automatically fill vertical gaps left by shorter items.
Assuming CSS Columns Preserve Row Order
CSS columns generally flow content vertically through columns. A masonry-style gallery built with columns may have a different visual reading sequence than a row-based grid.
Omitting a Fallback
Experimental layout features can be unsupported or change behavior. Provide a supported layout before applying experimental enhancements.
Ignoring Content Accessibility
Visual item placement may differ from DOM order. Keep meaningful content and keyboard navigation understandable without relying on visual position alone.
Important Considerations
Keep the following points in mind when working with masonry layouts:
- The historical
masonryshorthand is not a stable CSS property. - Current CSS Grid Level 3 work uses the grid-lanes layout model.
- Native masonry layout remains experimental and subject to specification changes.
- Masonry layouts reduce gaps caused by differently sized items.
- CSS Grid provides a dependable fallback with aligned rows.
- CSS columns provide a masonry-like alternative with column-first content flow.
- Responsive track sizing helps layouts adapt to different screen widths.
- Feature detection can help introduce experimental layout behavior progressively.
- DOM order, reading order, and keyboard navigation should remain understandable.
- Test experimental layouts in the actual browsers and devices your website supports.
Browser Support
The standalone CSS masonry shorthand is not Baseline and is not supported as an interoperable standard property across major browsers.
Native masonry layout remains experimental. Some browser development builds have implemented earlier syntax, while the current CSS Grid Level 3 draft describes the grid-lanes model.
For current compatibility information, visit Can I Use? and search for masonry.
Use supported CSS Grid or multi-column layouts as fallbacks. Do not assume experimental syntax works consistently across browsers.
Specifications
Native masonry layout is being developed as part of CSS Grid Layout Module Level 3. The specification describes the grid-lanes layout model rather than treating the earlier standalone masonry shorthand as a stable property.
CSS Grid Layout Module Level 3
For explanations, examples, and updated browser compatibility information, consult MDN Web Docs.
