Masonry-Style Layouts With the CSS That Ships Today

A masonry layout — items of varying heights packed into columns with no ragged gaps, as on image boards and some portfolio sites — is one of the most requested layouts CSS never had. A native masonry mode for grid has been specified and trialled, but it is not yet something to depend on across browsers. Meanwhile, two techniques that ship everywhere produce convincing masonry-style results, each with a trade-off in reading order. This page builds both, explains exactly what each does to the order in which items are seen and focused, and shows how to adopt native masonry as an enhancement when it arrives. It belongs to CSS Grid & Subgrid Layouts in the Mastering Container Queries & Responsive Layouts guide.

Why ordinary grids leave gaps

A regular CSS grid aligns items into rows and columns. Every item in a row shares that row's height, so a short card beside a tall one leaves empty space beneath it. That is the point of grid — alignment in two dimensions — and exactly what masonry gives up. Masonry keeps columns but abandons rows: each column is a stack of items, and each new item goes wherever a column is shortest.

Rows versus packed columns Left, a three-column grid where each row takes the height of its tallest item, leaving empty space under shorter items. Right, a masonry layout where each column stacks items of varied height with no gaps. Shared rows leave holes; packed columns do not regular grid dashed: empty space masonry no gaps between items

Technique 1: CSS multi-column layout

The simplest masonry-style layout uses multi-column layout, the same feature that sets newspaper columns. Items flow down the first column until it is full, then continue at the top of the next. Because each column is a continuous flow, items pack tightly with no gaps. The demo numbers the items and uses a 10rem column width so several columns fit in the small frame; watch how the numbers run down each column.

Live demoMasonry-style board with CSS multi-column
Drag the frame: the number of columns follows the width, items pack tightly, and each card stays whole. Notice the order runs down each column. Drag the bottom-right corner to resize the frame in either direction.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Multi-column masonry</title>
<style>
  body { font: 15px/1.5 system-ui, sans-serif; margin: 1rem; }

  .board {
    /* As many columns as fit at 14rem each; no media queries needed. */
    columns: 14rem;
    column-gap: 1rem;
  }

  .pin {
    /* Never split an item across two columns. */
    break-inside: avoid;
    margin-block-end: 1rem;       /* column-gap only applies horizontally */
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
  }
  .pin__img { display: block; width: 100%; border-radius: 6px; }
</style>
</head>
<body>
  <div class="board">
    <article class="pin"><div class="pin__img" style="aspect-ratio: 4/3; background:#bfdbfe"></div><p>Harbour</p></article>
    <article class="pin"><div class="pin__img" style="aspect-ratio: 3/4; background:#fde68a"></div><p>Lighthouse</p></article>
    <article class="pin"><div class="pin__img" style="aspect-ratio: 1; background:#c7d2fe"></div><p>Nets</p></article>
    <article class="pin"><div class="pin__img" style="aspect-ratio: 2/3; background:#bbf7d0"></div><p>Dunes</p></article>
    <article class="pin"><div class="pin__img" style="aspect-ratio: 16/9; background:#fecaca"></div><p>Pier</p></article>
    <article class="pin"><div class="pin__img" style="aspect-ratio: 3/4; background:#e9d5ff"></div><p>Shells</p></article>
  </div>
</body>
</html>

columns: 14rem means "columns at least 14rem wide, as many as fit", which makes the layout responsive to its container with no queries — narrow containers get one column, wide ones three or four. break-inside: avoid keeps each card whole. Vertical spacing uses a bottom margin because column-gap only separates columns.

The trade-off is order. Items run down column one, then column two. In a three-column board of twelve items, the second item a sighted user sees at the top of the page is item five, not item two. For collections without meaningful order — a mood board, a gallery — that is acceptable. For a chronological feed it is not.

Technique 2: grid with row spans

The second technique keeps grid's left-to-right placement and approximates masonry by giving each item a height measured in many small rows. The grid uses tiny row tracks, and each item spans as many as its content needs; grid-auto-flow: dense lets later items fill holes left by earlier ones.

.board {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 14rem), 1fr));
  grid-auto-rows: 8px;              /* a fine unit of height */
  grid-auto-flow: dense;            /* back-fill gaps with later items */
  column-gap: 1rem;
}

/* Each item's span is set from its content height, in 8px rows plus a
   gap allowance. Known-ratio media make this possible without script. */
.pin--s { grid-row: span 18; }      /* about 144px */
.pin--m { grid-row: span 26; }      /* about 208px */
.pin--l { grid-row: span 36; }      /* about 288px */

The spans must reflect each item's real height, which CSS cannot measure. When items are images of known aspect ratio, the span can be precomputed by the CMS as a size class or inline custom property; when items contain variable text, a small script measures each item and sets its span, which is the approach most "CSS grid masonry" libraries take. dense packing then changes the visual order in a different way from multi-column: later items jump back to fill earlier holes.

The key technique: understand each order

The deciding factor between the techniques is not appearance but order — the sequence in which sighted users scan the items versus the sequence in which keyboard focus and screen readers traverse them.

How each technique orders items Multi-column places items 1 to 3 down the first column and 4 to 6 down the second. Dense grid places items mostly across rows but moves a later item back into an earlier gap. Native masonry places each item in whichever column is currently shortest, roughly left to right. Same six items, three orders multi-column 1 2 3 4 5 down, then across dense grid 1 2 5 3 4 5 jumps back into a gap native masonry 1 2 3 4 shortest column next

Multi-column order is predictable — down each column in turn — but differs from the across-first scanning many users expect. Dense grid order is mostly across, with occasional jumps back, which is the least predictable of the three. Native masonry places each item in whichever column is currently shortest, which reads roughly left to right. In all three, keyboard focus follows the DOM, so a user tabbing through links experiences source order regardless of the visual arrangement. The more the visual order diverges from source order, the more disorienting that is — the concern behind WCAG 2.4.3 Focus Order.

Choosing between the techniques

The right technique depends on the content more than on the look.

Use multi-column when items are independent and unordered — mood boards, photo galleries, collections of quotes — and when the number of items is modest and fixed at render time. It needs no script, handles variable-height text perfectly because it measures nothing in advance, and its order, while column-first, is at least consistent.

Use grid with row spans when items should read roughly across in rows — a feed where newer items should appear near the top-left — and when you can supply heights, either as known image ratios from a CMS or from a small measuring script. Accept that dense will occasionally move an item backwards, and avoid it entirely for content whose order carries meaning.

Use a plain grid when neither trade-off is acceptable. A regular auto-fill grid with images cropped to a common aspect ratio has predictable row-major order and aligned rows. The visual gaps that masonry avoids are the price of that predictability, and for many product listings and news pages it is the better choice.

A useful test: ask whether a user would be confused if item seven appeared before item five. If yes, the content has an order and needs row-major placement without dense packing. If no, masonry's reordering is harmless.

Infinite scroll deserves a special mention. Appending items to a multi-column board reflows every column, so items the user has already seen jump to new positions — a jarring experience. Row-span grids and native masonry append at the bottom without moving existing items, which is why feeds that load more content should avoid the multi-column technique.

Adopting native masonry as an enhancement

When native masonry ships reliably, it can be layered over the multi-column baseline with a feature query, so browsers that support it get row-major masonry and others keep the column flow. Because the syntax has changed during specification, write the enhancement with whatever form your target browsers ship and guard it strictly.

/* Baseline everywhere: multi-column. */
.board { columns: 14rem; column-gap: 1rem; }
.pin { break-inside: avoid; margin-block-end: 1rem; }

/* Enhancement: only where the browser understands masonry rows.
   The whole block is ignored elsewhere, so the baseline stands. */
@supports (grid-template-rows: masonry) {
  .board {
    display: grid;
    columns: auto;
    grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr));
    grid-template-rows: masonry;
    gap: 1rem;
  }
  .pin { margin-block-end: 0; }
}

The enhancement resets columns and the bottom margin that only the baseline needed. When items enter the board — infinite scroll, filtering — keep motion light; staggered entrances like those in Staggered List Animations With Custom Properties look chaotic when items land in unpredictable columns, so a simple fade is usually better.

Browser support

CSS multi-column layout, including columns and break-inside, is supported in all current engines. CSS Grid with grid-auto-flow: dense and grid-auto-rows is supported in Chrome 57+, Edge 16+, Firefox 52+ and Safari 10.1+, and minmax() with min() in Chrome and Edge 79+, Firefox 75+ and Safari 13.1+. Native grid masonry has been available behind flags and in preview releases of several engines while its syntax settled; treat it as an enhancement guarded by @supports, and check the current status before relying on it.

FAQ

Can CSS do masonry layout natively? A native masonry mode for CSS Grid has been specified and has appeared behind flags and in preview builds, but it is not yet dependable across browsers. Production sites use CSS multi-column layout or grid techniques today, and can progressively enhance to native masonry where it ships.

What is the simplest CSS-only masonry? CSS multi-column layout: set columns: 16rem on the container and break-inside: avoid on the items. Items flow down the first column, then the second, packing tightly with no JavaScript. The catch is that reading order runs down columns, not across rows.

Does grid-auto-flow: dense create masonry? Not by itself. dense lets later items fill earlier gaps in a grid, which packs items of different spans tightly, but rows still have fixed heights. Combined with items that span different numbers of small rows, it approximates masonry at the cost of changing visual order.

Is masonry accessible? It can be, but every technique that packs items tightly risks separating visual order from DOM order, which confuses keyboard and screen-reader users. Keep items independent, avoid meaning that depends on sequence, and prefer techniques whose order is predictable.

Related articles

More pages in the same section.