Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Why line-height should be unitless in CSS

25 line-height without a unit, and two text tweaks

fair

Set line-height so it stays proportional when a descendant changes size. Then set small-caps-style headings with text-transform and open them up with letter-spacing.

Stuck? Three hints, smallest first
  1. 1. A unitless line-height is a multiplier and is re-computed per element. A px value is inherited as a fixed length.
  2. 2. The child at 24px inherits the factor 1.5 and gets 36px. With line-height: 24px it would still get 24px, and the lines would touch.
  3. 3. letter-spacing on uppercase text stops it looking cramped. Never letter-space lowercase body copy.

index.html

<div class="wrap">
  <p>Body copy at 16px with a line-height factor of 1.5, so 24px of leading.</p>
  <p class="big">A pull quote at 24px. The same factor gives it 36px, so it never
    collides with itself.</p>
  <h3 class="kicker">Departures board</h3>
</div>

style.css

.wrap {
  font-size: 16px;
  line-height: 1.5;      /* a factor, not a length: recomputed per element */
  max-width: 46ch;
}

.big {
  font-size: 24px;       /* inherits the 1.5, so 36px of leading */
}

/* Uppercase set solid looks cramped, so open it up a little. Applied to the
   transform, not to the source text, so the markup stays readable. */
.kicker {
  margin: 14px 0 0;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: #5c5c66;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 25

const factor = Number(/line-height:\s*([\d.]+)\s*;/.exec(css)[1]);
const base = Number(/\.wrap\s*\{\s*font-size:\s*(\d+)px/.exec(css)[1]);
const big = Number(/\.big\s*\{\s*font-size:\s*(\d+)px/.exec(css)[1]);

test('the line-height is a bare number', factor, 1.5);
test('body copy gets 24px of leading', factor * base, 24);
test('the pull quote gets 36px, from the same factor', factor * big, 36);
test('a fixed 24px would have left the quote cramped', factor * big === base * factor, false);
test('no unit is attached to line-height', /line-height:\s*[\d.]+(px|em|rem)/.test(css), false);
test('the kicker is uppercased in CSS, not in the markup', /text-transform:\s*uppercase/.test(css), true);
test('and the markup still reads normally', /Departures board/.test(html), true);
test('letter-spacing is in em, so it scales with the size', /letter-spacing:\s*[\d.]+em/.test(css), true);

Output


      

A unitless line-height is inherited as a factor and recomputed by each element from its own font size; a px value is inherited as a fixed length, so a 24px pull quote inside a line-height: 24px parent ends up with its lines touching. That is the whole reason resets use a bare number. On the typographic side, text-transform keeps the source text readable and lets a screen reader announce normal words — typing the heading in capitals would put shouting in the markup — and letter-spacing in em keeps the tracking proportional if the size changes.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.