Pinterest-style masonry layout using modern CSS Grid with dynamic item heights.
Pinterest-style masonry layout using modern CSS Grid with dynamic item heights. No JavaScript calculations needed for positioning!
CSS GRID
Pure CSS
No JS positioning
RESPONSIVE
Auto
Adapts to screen size
DYNAMIC
Any Size
Variable heights
Simple masonry layout with fixed column count and variable item heights.
Adjust the number of columns dynamically. Layout automatically recalculates.
Pinterest-style image gallery with hover effects and dynamic content.
Nature
Mountain Vista
Urban
City Lights
Nature
Ocean Waves
Nature
Forest Path
Landscape
Desert Dunes
Sky
Sunset Sky
Sky
Northern Lights
Nature
Tropical Beach
Nature
Autumn Leaves
Landscape
Ice Cave
Nature
Lavender Field
Nature
Cherry Blossoms
Set Grid Auto Rows
Use small row height (e.g., 10px) to create fine-grained grid
grid-auto-rows: 10pxCalculate Row Span
Divide item height by row height to get number of rows to span
rowSpan = Math.ceil(height / 10)Apply Grid Row End
Set grid-row-end to span the calculated number of rows
grid-row-end: span {rowSpan}Grid Auto-Placement
CSS Grid automatically positions items to fill gaps efficiently
.masonry-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 10px;
gap: 16px;
}
.masonry-item {
grid-row-end: span var(--row-span);
}
/* Calculate row span with JavaScript */
const rowSpan = Math.ceil(
(itemHeight + gap) / (rowHeight + gap)
);
item.style.setProperty('--row-span', rowSpan);✓ Image Galleries
Pinterest-style photo layouts
✓ Blog Cards
Article previews with varying content
✓ Product Grids
E-commerce product displays
✓ Portfolio Layouts
Showcase work with dynamic sizing
.masonry-grid {
display: grid;
grid-template-columns: repeat(
auto-fill,
minmax(250px, 1fr)
);
grid-auto-rows: 10px;
gap: 16px;
}
.masonry-item {
grid-row-end: span var(--row-span);
}
/* Calculate row span based on content height */
const item = document.querySelector('.masonry-item')
const rowHeight = 10
const rowGap = 16
const rowSpan = Math.ceil(
(item.offsetHeight + rowGap) / (rowHeight + rowGap)
)
item.style.setProperty('--row-span', rowSpan)