CSS & vertical rhythm for text, images, and tables

Vincent Bernat

Ver­ti­cal rhythm aligns lines to a con­sis­tent spac­ing ca­dence down the page. It cre­ates a pre­dictable flow for the eye to fol­low. Thanks to the rlh CSS unit, ver­ti­cal rhythm is now eas­ier to im­ple­ment for text.1 But il­lus­tra­tions and ta­bles can dis­rupt the lay­out. The am­a­teur ty­pog­ra­pher in me wants to fol­low Bringhurst’s wisdom:

Head­ings, sub­heads, block quo­ta­tions, foot­notes, il­lus­tra­tions, cap­tions and other in­tru­sions into the text cre­ate syn­co­pa­tions and vari­a­tions against the base rhythm of reg­u­larly leaded lines. These vari­a­tions can and should add life to the page, but the main text should also re­turn af­ter each vari­a­tion pre­cisely on beat and in phase.

― Robert Bringhurst, The El­e­ments of Ty­po­graphic Style

Text#

Three fac­tors gov­ern ver­ti­cal rhythm: font size, line height and mar­gin or pad­ding. Let’s set our base­line with an 18-pixel font and a 1.5 line height:

html {
  font-size: 112.5%;
  line-height: 1.5;
}
h1, h2, h3, h4 {
  font-size: 100%;
}
html, body,
h1, h2, h3, h4,
p, blockquote,
dl, dt, dd, ol, ul, li {
  margin: 0;
  padding: 0;
}

CSS Val­ues and Units Mod­ule Level 4 de­fines the rlh unit, equal to the com­puted line height of the root el­e­ment. All browsers sup­port it since 2023.2 Use it to in­sert ver­ti­cal spa­ces or to fix the line height when al­ter­ing font size:3

h1, h2, h3, h4 {
  margin-top: 2rlh;
  margin-bottom: 1rlh;
}
h1 {
  font-size: 2.5rem;
  line-height: 2rlh;
}
h2 {
  font-size: 1.5rem;
  line-height: 1rlh;
}
h3 {
  font-size: 1.25rem;
  line-height: 1rlh;
}
p, blockquote, pre {
  margin-top: 1rlh;
}
aside {
  font-size: 0.875rem;
  line-height: 1rlh;
}

We can check the re­sult by over­lay­ing a grid4 on the content:

Screenshot of my website with a grid as an overlay and each line of text
fitting on the grid
Using CSS rlh unit to set vertical space works well for text. You can display the grid by typing ← ← g.

If a child el­e­ment uses a font with taller in­trin­sic met­rics, it may stretch the line’s box be­yond the con­fig­ured line height.5 A work­around is to re­duce the line height to 1. The glyphs over­flow but don’t push the line taller.

code, kbd {
  line-height: 1;
}

Responsive images#

Re­spon­sive im­ages are dif­fi­cult to align on the grid be­cause we don’t know their height. CSS Rhyth­mic Siz­ing Mod­ule Level 1 in­tro­duces the block-step prop­erty to ad­just the height of an el­e­ment to a mul­ti­ple of a step unit. But most browsers don’t sup­port it yet.

With Java­Script, we can add pad­ding around the im­age so it does not dis­turb the ver­ti­cal rhythm:

const targets = document.querySelectorAll(".lf-media-outer");
const adjust = (el, height) => {
  const rlh = parseFloat(getComputedStyle(document.documentElement).lineHeight);
  const padding = Math.ceil(height / rlh) * rlh - height;
  el.style.padding = `${padding / 2}px 0`;
};

targets.forEach((el) => adjust(el, el.clientHeight));
Screenshot of my website with a grid as an overlay and an image not breaking
the vertical rhythm. Additional padding is visible before and after the image.
The height of the image with padding is
216.
The image is snapped to the grid thanks to the additional padding computed with JavaScript. 216 is divisible by 27, our line height in this example.

As the im­age is re­spon­sive, its height can change. We need to wrap a re­size ob­server around the adjust() function:

const ro = new ResizeObserver((entries) => {
  for (const entry of entries) {
    const height = entry.contentBoxSize[0].blockSize;
    adjust(entry.target, height);
  }
});
for (const target of targets) {
  ro.observe(target);
}

Tables#

Ta­ble cells could set 1rlh as their height but they would feel con­stricted. Us­ing 2rlh wastes too much space. In­stead, we use in­cre­men­tal lead­ing: we align one in ev­ery five lines.

table {
  border-spacing: 2px 0;
  border-collapse: separate;
  th {
    padding: 0.4rlh 1em;
  }
  td {
    padding: 0.2rlh 0.5em;
  }
}

To align the el­e­ments af­ter the ta­ble, we need to add some pad­ding. We can ei­ther re­use the Java­Script code from im­ages or use a few lines of CSS that count the reg­u­lar rows and com­pute the miss­ing ver­ti­cal padding:

table:has(tbody tr:nth-child(5n):last-child)   { padding-bottom: 0.2rlh; }
table:has(tbody tr:nth-child(5n+1):last-child) { padding-bottom: 0.8rlh; }
table:has(tbody tr:nth-child(5n+2):last-child) { padding-bottom: 0.4rlh; }
table:has(tbody tr:nth-child(5n+3):last-child) { padding-bottom: 0 }
table:has(tbody tr:nth-child(5n+4):last-child) { padding-bottom: 0.6rlh; }

A header cell has twice the pad­ding of a reg­u­lar cell. With two reg­u­lar rows, the to­tal pad­ding is 2×2×0.2+2×0.4=1.6. We need to add 0.4rlh to reach 2rlh of ex­tra ver­ti­cal pad­ding across the table.

Screenshot of my website with a grid as an overlay and a table following the
vertical rhythm. Additional padding is visible after the table. The height of
the table with padding is 405.
One line out of five is aligned to the grid. Additional padding is added after the table to not break the vertical rhythm. 405 is divisible by 27, our line height in this example.

None of this is nec­es­sary. But once you start look­ing, you can’t un­see it. Un­til browsers im­ple­ment CSS Rhyth­mic Siz­ing, a bit of CSS wiz­ardry and a touch of Java­Script is enough to pull it off. The main text now re­turns af­ter each in­tru­sion “pre­cisely on beat and in phase.” 🎼