← Back to HTML & CSS, page 7

html-css-0131 · Medium mid

How can you make a masonry-style layout of different-height cards with CSS columns, and what is the catch?

Use column-width or column-count with column-gap and break-inside: avoid on each card. Items fill each column from top to bottom, so the reading and focus order is not left to right, which makes it unsuitable for ordered content.

Full answer

CSS multi-column layout splits content into columns like a newspaper. With it, a masonry look needs very little code.

  • column-count: 3 sets the number of columns. column-width: 200px instead lets the browser fit as many columns of at least that width as possible, which is responsive without media queries.
  • column-gap sets the space between columns.
  • Items flow down the first column, then the next one. To keep a card whole, set break-inside: avoid on each card. Without it, a card can be split between two columns.
  • Cards need a bottom margin to space them, since gap only works between columns.

The catch is the order. Items go top to bottom in the first column, then continue at the top of the second column. A grid of photos that should read left to right will be in a different order than the visitor expects, and keyboard focus follows the same order, so Tab moves down a column and then up to the next. For content where the order matters, such as search results or steps, this layout is a poor choice. It suits independent items such as a photo wall or notes.

Other facts:

  • A tall item cannot span columns, except with column-span: all, which spans every column.
  • The column heights are balanced by the browser.
  • CSS Grid and Flexbox give you a left-to-right order, but not a true masonry layout by themselves. A native masonry feature for grid is still new, so check the support table before using it. JavaScript libraries can place cards by measuring them, at some cost to speed and complexity.

With 400px and a 120px column width, there are three columns. The browser balances their heights: cards 1 and 2 are stacked in the first column, card 3 is alone in the second, and cards 4 and 5 are stacked in the third. The reading order goes down each column, not across.

Pick this layout only when the order of items does not matter.

html
<style>
  .wall { column-width: 120px; column-gap: 12px; width: 400px; }
  .card { break-inside: avoid; margin: 0 0 12px; padding: 8px; background: #eef; font: 14px sans-serif; }
</style>
<div class="wall" id="wall">
  <div class="card" id="c1" style="height:80px">1</div>
  <div class="card" id="c2" style="height:40px">2</div>
  <div class="card" id="c3" style="height:100px">3</div>
  <div class="card" id="c4" style="height:50px">4</div>
  <div class="card" id="c5" style="height:60px">5</div>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • column-count or column-width creates columns.
  • break-inside: avoid keeps a card whole.
  • Spacing inside a column needs margins.
  • Order runs down each column, not across.
  • Native grid masonry is new; check support.

Common mistakes

  • Using columns for content that has a meaningful left-to-right order.
  • Forgetting break-inside: avoid, so cards are cut across columns.
  • Expecting row-gap or gap to space items inside a column.

Follow-ups

  • Why does keyboard order matter here?
  • How does column-width differ from column-count?

Tips

  • Say that columns make a masonry look easily, then state the order problem.
  • Mention that native masonry for grid is new, with the support caveat.