SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/masonry-layouts

7 CSS Masonry Layouts

7 Pure CSSPublished 2026-10-05Free · no sign-up

Seven ways to build masonry: CSS columns, a grid with row spans, native masonry with a columns fallback, a pin board of image cards, justified photo rows, a wall of quotes of different lengths and a grid masonry with a featured tile spanning two columns.

01. Masonry with CSS Columns

MasonryColumns

The simplest masonry: column-width makes as many columns as fit, items flow down each column and break-inside: avoid stops a card being split. Reading order runs down the first column then the next, which suits galleries better than ranked lists.

View code
<ul class="ms1"><li style="--h:110px">1</li><li style="--h:70px">2</li><li style="--h:140px">3</li><li style="--h:90px">4</li><li style="--h:120px">5</li><li style="--h:80px">6</li><li style="--h:100px">7</li></ul>
.ms1 { width: 460px; max-width: 100%; margin: 0; padding: 0; list-style: none; column-width: 120px; column-gap: 10px; font: 800 16px Inter, system-ui, sans-serif; }
.ms1 li { display: grid; place-items: center; height: var(--h); margin-bottom: 10px; border-radius: 12px; background: #e0e7ff; color: #3730a3; break-inside: avoid; }
.ms1 li:nth-child(3n) { background: #fce7f3; color: #9d174d; } .ms1 li:nth-child(3n+2) { background: #dcfce7; color: #166534; }

02. Grid Masonry with Row Spans

MasonryGrid

A grid with small, fixed row tracks where each item spans a number of rows matching its height, plus grid-auto-flow: dense to backfill gaps. Unlike columns, items read left to right, row by row, which keeps their order meaningful.

View code
<ul class="ms2"><li style="--s:6">1</li><li style="--s:4">2</li><li style="--s:8">3</li><li style="--s:5">4</li><li style="--s:7">5</li><li style="--s:4">6</li></ul>
.ms2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); grid-auto-rows: 14px; grid-auto-flow: dense; gap: 0 10px; width: 420px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 800 16px Inter, system-ui, sans-serif; }
.ms2 li { grid-row: span var(--s); display: grid; place-items: center; margin-bottom: 10px; border-radius: 12px; background: #1e293b; color: #a5b4fc; border: 1px solid #334155; }

03. Native Masonry with a Fallback

MasonryProgressive

Browsers that support grid-template-rows: masonry get true masonry from one line, with items placed in order into the shortest column. Everywhere else, @supports falls back to CSS columns, so the layout never breaks while the standard settles.

View code
<ul class="ms3"><li style="--h:100px">1</li><li style="--h:60px">2</li><li style="--h:130px">3</li><li style="--h:80px">4</li><li style="--h:110px">5</li><li style="--h:70px">6</li></ul>
.ms3 { width: 420px; max-width: 100%; margin: 0; padding: 0; list-style: none; column-width: 110px; column-gap: 10px; font: 800 16px Inter, system-ui, sans-serif; }
.ms3 li { display: grid; place-items: center; height: var(--h); margin-bottom: 10px; border-radius: 12px; background: #fef3c7; color: #92400e; break-inside: avoid; }
@supports (grid-template-rows: masonry) { .ms3 { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); grid-template-rows: masonry; gap: 10px; columns: auto; } .ms3 li { margin: 0; } }

04. Pin Board Cards

MasonryCards

Pinterest-style cards of different heights, each with an image area and a caption, flowing in CSS columns. The images keep their own aspect ratios, which is what makes the board look natural, and the captions stay attached because cards never split.

View code
<div class="ms4"><figure><div style="--a:3/4;--g:linear-gradient(160deg,#fde68a,#f97316)"></div><figcaption>Autumn table setting</figcaption></figure><figure><div style="--a:1;--g:linear-gradient(160deg,#bae6fd,#2563eb)"></div><figcaption>Blue glaze test</figcaption></figure><figure><div style="--a:4/3;--g:linear-gradient(160deg,#bbf7d0,#15803d)"></div><figcaption>Herb garden</figcaption></figure><figure><div style="--a:2/3;--g:linear-gradient(160deg,#fbcfe8,#9d174d)"></div><figcaption>Flower stall</figcaption></figure><figure><div style="--a:1;--g:linear-gradient(160deg,#ddd6fe,#6d28d9)"></div><figcaption>Studio wall</figcaption></figure></div>
.ms4 { width: 460px; max-width: 100%; column-width: 130px; column-gap: 12px; font: 600 12px Inter, system-ui, sans-serif; color: #344054; }
.ms4 figure { margin: 0 0 12px; break-inside: avoid; }
.ms4 figure div { aspect-ratio: var(--a); border-radius: 14px; background: var(--g); }
.ms4 figcaption { padding: 6px 2px 0; }

05. Justified Rows

MasonryFlexbox

The horizontal cousin of masonry: rows of equal height where items of different widths stretch to fill each row exactly, like a photo library. Each item's flex-grow is its aspect ratio, so wide images claim more of the row.

View code
<ul class="ms5"><li style="--r:1.5;--g:#f97316"></li><li style="--r:.75;--g:#22d3ee"></li><li style="--r:1;--g:#a3e635"></li><li style="--r:1.8;--g:#f472b6"></li><li style="--r:.8;--g:#818cf8"></li><li style="--r:1.3;--g:#fbbf24"></li><li class="end"></li></ul>
.ms5 { display: flex; flex-wrap: wrap; gap: 6px; width: 440px; max-width: 100%; margin: 0; padding: 0; list-style: none; }
.ms5 li { flex-grow: calc(var(--r) * 100); flex-basis: calc(var(--r) * 80px); height: 80px; border-radius: 8px; background: var(--g); }
.ms5 .end { flex-grow: 1000; height: 0; }

06. Masonry Testimonial Wall

MasonryTestimonials

Quotes of very different lengths look best in masonry, because a regular grid would leave big gaps under the short ones. CSS columns pack them neatly, and each quote is a figure with its attribution so the wall is still a sensible list when read aloud.

View code
<div class="ms6"><figure><blockquote>Fast.</blockquote><figcaption>Ola</figcaption></figure><figure><blockquote>The team rebuilt our checkout in a week and our mobile sales went up the month after.</blockquote><figcaption>Priya, Brew Lab</figcaption></figure><figure><blockquote>Clear pricing and no surprises.</blockquote><figcaption>Jonas</figcaption></figure><figure><blockquote>They explained every decision, which made the whole project easier to approve internally.</blockquote><figcaption>Dana, Northwind</figcaption></figure></div>
.ms6 { width: 460px; max-width: 100%; column-width: 150px; column-gap: 10px; font: 14px/1.5 Inter, system-ui, sans-serif; }
.ms6 figure { margin: 0 0 10px; padding: 14px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; break-inside: avoid; }
.ms6 blockquote { margin: 0; color: #101828; }
.ms6 figcaption { margin-top: 6px; color: #475467; font-size: 12px; font-weight: 600; }

07. Masonry with a Featured Item

MasonryGrid

A grid-based masonry where the first item spans two columns as a featured tile, something CSS columns cannot do. Row spans set the heights, and dense packing fills the spaces around the big tile.

View code
<ul class="ms7"><li class="big" style="--s:9">Featured</li><li style="--s:5">2</li><li style="--s:7">3</li><li style="--s:4">4</li><li style="--s:6">5</li><li style="--s:5">6</li></ul>
.ms7 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: 12px; grid-auto-flow: dense; column-gap: 8px; width: 420px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 800 15px Inter, system-ui, sans-serif; }
.ms7 li { grid-row: span var(--s); display: grid; place-items: center; margin-bottom: 8px; border-radius: 12px; background: #1e293b; color: #cbd5e1; }
.ms7 .big { grid-column: span 2; background: linear-gradient(135deg, #7c3aed, #db2777); color: #fff; }

About these CSS Masonry Layouts

Columns: simplest, column order

column-width with break-inside: avoid on items is the easiest masonry. Items flow down the first column before the next, so it suits galleries but not lists where order matters.

Grid spans: row order

With small grid-auto-rows and each item spanning rows to match its height, items read left to right. Add grid-auto-flow: dense to backfill gaps, knowing it can move items out of source order.

Native masonry is coming

grid-template-rows: masonry places items into the shortest column in source order with one line. Use it inside @supports with columns as the fallback until support is broad.

Keep source order sensible

Keyboard and screen reader users follow the HTML order, not the visual one. Put the most important items first in the markup whichever technique you choose.

Frequently asked questions

Do I need a JavaScript masonry library?

Rarely now. CSS columns or grid spans cover most cases, and native masonry is arriving.

How do I know the row span for each item?

For fixed content you can set it by hand; for images, calculate it from the image's aspect ratio.

What is a justified layout?

Rows of equal height whose items stretch to fill each row, like photo libraries. Flex-grow based on aspect ratio does it.