Masonry Columns Layout
Cards of uneven height packed into 2, 3 or 4 columns with CSS columns, showing the top-to-bottom reading order.
AI prompt
Clean web gallery layout in a masonry style: three columns of rounded cards of different heights packed tightly with even 8px gaps and no empty holes, each card a soft pastel colour block with a small number in the corner, numbers running down the first column and then continuing at the top of the second, light grey page background, flat modern UI, crisp edges. Variants: two wide columns and four narrow columns. Negative: overlapping cards, uneven gaps, shadows, photos, text errors. Reduced motion: static image.About this pattern
Definition
A masonry layout packs cards of different heights into columns with no row gaps, like bricks. With plain CSS you can get it using column-count: items flow down the first column, then the next, which is cheap and robust but changes the reading order.
Purpose & UX principles
- column-count sets how many columns; column-gap and a bottom margin set the spacing.
- break-inside: avoid stops a card from splitting across two columns.
- Order runs down each column, not across; number the cards to see it.
- Use it for images and notes of unequal height, not for ranked lists.
Interaction states
- Three column presets and a Gap slider with numbered cards.
Motion specification
- The layout reflows instantly.
- Reduced motion: nothing animates.
Responsive behaviour
- Change the column count at breakpoints: one on phones, two on tablets, three or more on desktop.
- Use column-width instead of a count to let the browser choose.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Columns"><button data-p="0" aria-pressed="true">3 columns</button><button data-p="1" aria-pressed="false">2 columns</button><button data-p="2" aria-pressed="false">4 columns</button></div><div class="ms" id="ms"></div><label class="rw">Gap <b id="gv">8px</b><input id="gp" type="range" min="2" max="20" value="8"></label><p class="out" id="out"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ms{column-count:var(--n);column-gap:var(--g)}.ms div{break-inside:avoid;margin-bottom:var(--g);border-radius:8px;padding:6px 8px;font-size:11px;font-weight:700;color:rgba(0,0,0,.55)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const N=[3,2,4],H=[44,28,52,34,24,48,30,40],CL=['#f6d7b8','#cfe5f5','#d9efc9','#f4cfe0','#e1d6f5','#fbeaa6','#c7ece4','#f8c9c0'],ms=document.getElementById('ms'),gp=document.getElementById('gp'),bs=[...document.querySelectorAll('#pre button')];let p=0;
ms.innerHTML=H.map((h,i)=>'<div style="height:'+h+'px;background:'+CL[i]+'">'+(i+1)+'</div>').join('');
function up(){ms.style.setProperty('--n',N[p]);ms.style.setProperty('--g',gp.value+'px');document.getElementById('gv').textContent=gp.value+'px';document.getElementById('out').textContent='Reading order runs down each column, not across.';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));gp.addEventListener('input',up);up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Masonry Columns Layout — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ms{column-count:var(--n);column-gap:var(--g)}.ms div{break-inside:avoid;margin-bottom:var(--g);border-radius:8px;padding:6px 8px;font-size:11px;font-weight:700;color:rgba(0,0,0,.55)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Columns"><button data-p="0" aria-pressed="true">3 columns</button><button data-p="1" aria-pressed="false">2 columns</button><button data-p="2" aria-pressed="false">4 columns</button></div><div class="ms" id="ms"></div><label class="rw">Gap <b id="gv">8px</b><input id="gp" type="range" min="2" max="20" value="8"></label><p class="out" id="out"></p></div>
</div>
<script>
const N=[3,2,4],H=[44,28,52,34,24,48,30,40],CL=['#f6d7b8','#cfe5f5','#d9efc9','#f4cfe0','#e1d6f5','#fbeaa6','#c7ece4','#f8c9c0'],ms=document.getElementById('ms'),gp=document.getElementById('gp'),bs=[...document.querySelectorAll('#pre button')];let p=0;
ms.innerHTML=H.map((h,i)=>'<div style="height:'+h+'px;background:'+CL[i]+'">'+(i+1)+'</div>').join('');
function up(){ms.style.setProperty('--n',N[p]);ms.style.setProperty('--g',gp.value+'px');document.getElementById('gv').textContent=gp.value+'px';document.getElementById('out').textContent='Reading order runs down each column, not across.';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));gp.addEventListener('input',up);up();
</script>
</body></html>