Vertical Rhythm & Baseline Grid
Text snapped to an 8px baseline grid versus text with arbitrary spacing, with the grid overlay switchable.
AI prompt
Editorial layout mockup of a single text column on a warm white page with a faint horizontal baseline grid every 8 pixels drawn in pale blue, a bold heading and two short paragraphs whose every line sits exactly on the grid lines, equal gaps between blocks, small labels showing line-height 24px and gap 16px in the margin, flat design, no images. Negative: lines between grid lines, uneven gaps, decorative fonts, shadows, text errors. Reduced motion: static layout.About this pattern
Definition
Vertical rhythm means every line height and gap is a multiple of one base unit. Lines then fall on a regular invisible grid, which makes columns and blocks line up and makes the page feel calm even when you cannot say why.
When to use it
- Choose a unit (4 or 8px) and make line heights and margins multiples of it.
- Round the line height up to the next multiple: a 15px font with a 1.5 ratio becomes 24px on an 8px grid.
- The off-grid version drifts a little every paragraph, which is visible when two columns sit side by side.
What the controls do
- Snap on or off, unit 4 or 8px, and a Show grid toggle.
Motion & timing
- Changes apply instantly; no animation.
- Reduced motion: nothing to change.
Tips & variations
- Use rem or a CSS variable for the unit so it scales with user font size.
- Images and cards should also have heights in multiples of the unit.
Code of this live preview
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Spacing"><button data-p="0" aria-pressed="true">Snapped to grid</button><button data-p="1" aria-pressed="false">Off the grid</button></div><div class="row2"><div class="pre" id="un" role="group" aria-label="Unit"><button data-u="4" aria-pressed="false">4px</button><button data-u="8" aria-pressed="true">8px</button></div><label class="sw"><input id="gd" type="checkbox" checked> Show grid</label></div><div class="tx" id="tx"><h4>Quiet rhythm</h4><p>Lines that share a grid look calm.</p><p>Gaps use the same unit, so blocks line up.</p></div><p class="out" id="out" aria-live="polite"></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}
.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}
.row2{display:flex;justify-content:space-between;align-items:center}.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
.tx{position:relative;height:118px;border:1px solid var(--line);border-radius:10px;background:var(--surface);padding:0 12px;overflow:hidden;background-image:repeating-linear-gradient(to bottom,transparent 0,transparent calc(var(--u) - 1px),var(--gl) calc(var(--u) - 1px),var(--gl) var(--u))}
.tx h4{font-size:20px;line-height:var(--lh1);margin:var(--mt) 0 0}.tx p{font-size:14px;line-height:var(--lh);margin:var(--mb) 0 0;color:#444}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const tx=document.getElementById('tx'),gd=document.getElementById('gd'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')],un=[...document.querySelectorAll('#un button')];let p=0,u=8;
function up(){const sn=p===0,r=n=>Math.ceil(n/u)*u;tx.style.setProperty('--u',u+'px');tx.style.setProperty('--gl',gd.checked?'rgba(60,110,255,.22)':'transparent');
tx.style.setProperty('--lh1',sn?r(20*1.2)+'px':'1.2');tx.style.setProperty('--lh',sn?r(14*1.5)+'px':'1.5');tx.style.setProperty('--mt',sn?u*2+'px':'13px');tx.style.setProperty('--mb',sn?u+'px':'9px');
out.innerHTML=sn?'<b>Snapped:</b> line height '+r(14*1.5)+'px and gaps are multiples of '+u+'px, so every line lands on a grid line.':'<b>Off the grid:</b> line height 21px and gaps of 13px and 9px drift away from the lines.';
bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));un.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.u===u)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));un.forEach(b=>b.addEventListener('click',()=>{u=+b.dataset.u;up();}));gd.addEventListener('change',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>Vertical Rhythm & Baseline Grid — 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}}
.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}
.row2{display:flex;justify-content:space-between;align-items:center}.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}
.tx{position:relative;height:118px;border:1px solid var(--line);border-radius:10px;background:var(--surface);padding:0 12px;overflow:hidden;background-image:repeating-linear-gradient(to bottom,transparent 0,transparent calc(var(--u) - 1px),var(--gl) calc(var(--u) - 1px),var(--gl) var(--u))}
.tx h4{font-size:20px;line-height:var(--lh1);margin:var(--mt) 0 0}.tx p{font-size:14px;line-height:var(--lh);margin:var(--mb) 0 0;color:#444}
@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="Spacing"><button data-p="0" aria-pressed="true">Snapped to grid</button><button data-p="1" aria-pressed="false">Off the grid</button></div><div class="row2"><div class="pre" id="un" role="group" aria-label="Unit"><button data-u="4" aria-pressed="false">4px</button><button data-u="8" aria-pressed="true">8px</button></div><label class="sw"><input id="gd" type="checkbox" checked> Show grid</label></div><div class="tx" id="tx"><h4>Quiet rhythm</h4><p>Lines that share a grid look calm.</p><p>Gaps use the same unit, so blocks line up.</p></div><p class="out" id="out" aria-live="polite"></p></div>
</div>
<script>
const tx=document.getElementById('tx'),gd=document.getElementById('gd'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')],un=[...document.querySelectorAll('#un button')];let p=0,u=8;
function up(){const sn=p===0,r=n=>Math.ceil(n/u)*u;tx.style.setProperty('--u',u+'px');tx.style.setProperty('--gl',gd.checked?'rgba(60,110,255,.22)':'transparent');
tx.style.setProperty('--lh1',sn?r(20*1.2)+'px':'1.2');tx.style.setProperty('--lh',sn?r(14*1.5)+'px':'1.5');tx.style.setProperty('--mt',sn?u*2+'px':'13px');tx.style.setProperty('--mb',sn?u+'px':'9px');
out.innerHTML=sn?'<b>Snapped:</b> line height '+r(14*1.5)+'px and gaps are multiples of '+u+'px, so every line lands on a grid line.':'<b>Off the grid:</b> line height 21px and gaps of 13px and 9px drift away from the lines.';
bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));un.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.u===u)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));un.forEach(b=>b.addEventListener('click',()=>{u=+b.dataset.u;up();}));gd.addEventListener('change',up);up();
</script>
</body></html>