Masonry Layout
Tue Mar 18 2025
Introduction
Masonry layout is a popular layout technique used in web design to create a grid of items with varying heights. It’s often used in galleries, blogs, and portfolios to display content in a visually appealing way.
Demo example











How to Create
---
// component.astro
---
<div class="masonry-container">
<figure class="masonry-item">
<img src="https://assets.codepen.io/12005/windmill.jpg" />
<figcaption><a href="#">1</a></figcaption>
</figure>
<figure class="masonry-item">
<img src="https://assets.codepen.io/12005/suspension-bridge.jpg" />
<figcaption><a href="#">2</a></figcaption>
</figure>
</div>
<style>
.masonry-container {
column-count: 4;
column-gap: 16px;
width: 100%;
}
.masonry-item {
margin: 0;
display: grid;
grid-template-rows: 1fr auto;
margin-bottom: 16px;
break-inside: avoid;
width: 100%;
}
.masonry-item img {
grid-row: 1 / -1;
grid-column: 1;
width: 100%;
max-width: 100%;
}
.masonry-item figcaption {
grid-row: 2;
grid-column: 1;
background-color: rgba(0,0,0,0.7);
color: white;
padding: .2em .5em;
justify-self: start;
}
.masonry-item figcaption a {
color: white;
text-decoration: none;
}
@media (max-width: 1200px) {
.masonry-container {
column-count: 3;
}
}
@media (max-width: 800px) {
.masonry-container {
column-count: 2;
}
}
@media (max-width: 500px) {
.masonry-container {
column-count: 1;
}
}
</style>