Creating a masonry layout
I'm planning to design a masonry-like layout using CSS. I’ll craft an HTML snippet and CSS while ensuring it's responsive, using media queries or auto columns, and allows for dynamic heights. Plus, I need to mention potential issues like reading order. Although I could suggest using the masonry value in CSS grid, since it’s not widely supported, I'll focus on pure CSS.
Here's a minimal HTML and CSS example for a masonry layout that enhances user experience without JavaScript.Implementing a CSS-only masonry layout
I need to explain the use of break-inside: avoid-column for browsers like Safari, where it might be just break-inside: avoid. It's important to mention that not all browsers handle break-inside consistently, so I should include a fallback for older browsers.
The user wants a CSS-only masonry layout that adjusts to dynamic content sizes. I'll show final code with the HTML structure and necessary CSS. Plus, I’ll note that the layout adapts automatically, maintaining the DOM order for accessibility. I'll also use @media queries for larger screens to ensure versatility.Addressing dynamic content
I want to keep in mind that the user may need an explanation about dynamic content and its unknown heights. When using multi-columns, these dynamic items can be treated as if they were on multiple pages. I should provide a clear and concise answer that explains how this layout accommodates such content while ensuring a pleasant user experience. My goal is to give a straightforward explanation that captures these important aspects without overwhelming them with details. Here’s a fully CSS-only masonry layout that automatically adapts to dynamic item heights using CSS Multi-column Layout.
HTML
<div class="masonry">
<article class="masonry-item">
<h2>Card 1</h2>
<p>Short content.</p>
</article>
<article class="masonry-item">
<h2>Card 2</h2>
<p>
Much longer content goes here to show how cards with different heights
flow naturally into a masonry layout without JS.
</p>
</article>
<article class="masonry-item">
<h2>Card 3</h2>
<img src="https://via.placeholder.com/400x250" alt="">
</article>
<!-- add as many .masonry-item elements as you like -->
</div>
CSS (masonry layout)
:root {
--masonry-gap: 1rem;
--masonry-min-column-width: 220px; /* min width of each column */
}
.masonry {
/* Create responsive columns */
column-width: var(--masonry-min-column-width);
column-gap: var(--masonry-gap);
/* Optional: nicer default */
widows: 1;
orphans: 1;
}
/* The individual bricks */
.masonry-item {
display: inline-block; /* needed so items respect columns */
width: 100%; /* fill the column width */
margin: 0 0 var(--masonry-gap);
box-sizing: border-box;
/* Prevent items splitting between columns */
break-inside: avoid-column;
-webkit-column-break-inside: avoid;
-moz-column-break-inside: avoid;
/* Just some styling */
background: #fff;
border-radius: 8px;
padding: 1rem;
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}
/* Simple responsive tuning (optional) */
@media (max-width: 600px) {
.masonry {
column-width: 100%; /* single column on very small screens */
}
}
How it works
.masonry uses column-width to create as many columns as fit the container; the layout automatically reflows as the viewport changes.
- Each
.masonry-item can have any height (dynamic content, images, different text lengths) and the browser will stack them in a masonry-like fashion.
break-inside: avoid-column keeps each item intact within a column so it doesn’t split across columns.
This is widely supported and requires no JavaScript.