Sub topic 13
Line height, letter spacing, and word spacing
This chapter assumes you know CSS selectors and the cascade, the box model, and how to set font size with rem and em. Here you will control the vertical rhythm and horizontal texture of text with three properties: line-height, letter-spacing, and word-spacing.
Line height
line-height sets the height of each line box. The extra space beyond the glyphs is split evenly above and below the text, which is called half-leading.
The property accepts several value types, and they behave differently when inherited.
| Value | Example | What children inherit |
|---|---|---|
| Unitless number | `1.5` | The number itself. Each element multiplies it by its own font size. |
| Length | `24px`, `1.5em` | The computed length. Children use the same absolute value. |
| Percentage | `150%` | The computed length, like a length. |
| `normal` | `normal` | The keyword. The browser picks a value from the font, usually about 1.2. |
Because of this inheritance rule, prefer a unitless number.
body {
font-size: 1rem;
line-height: 1.5;
}
small {
font-size: 0.75rem;
}The small element inherits 1.5 and computes 0.75rem × 1.5 for its line height. If the body used line-height: 150% instead, the body's computed length (24px at a 16px font size) would pass down, and the smaller text would get a line height that is too large for it. The reverse problem also occurs: a heading with a larger font size could inherit a line height that is too small, and its lines would overlap.
Choosing a value
- Body text reads well at roughly 1.4 to 1.6. WCAG 2.1 success criterion 1.4.12 requires that content still works when users set line height to at least 1.5 times the font size, so avoid fixed heights on text containers.
- Large headings need less, often 1.1 to 1.25, because the same ratio produces very large gaps at big sizes.
- Long lines need more line height than short ones, because the eye must travel farther to find the next line.
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
line-height: 1.15;
}Letter spacing
letter-spacing adds space after every character. It takes a length, and normal means no extra space. Negative values tighten the text.
Use em units so the spacing scales with the font size.
.eyebrow {
text-transform: uppercase;
letter-spacing: 0.08em;
font-size: 0.875rem;
}Uppercase text usually benefits from a little added spacing, around 0.05em to 0.1em. Body text should be left alone. Fonts are already designed with spacing for lowercase reading, and changing it often hurts legibility. Be careful with negative values on large headings: small amounts such as -0.02em can look tidy, but too much makes letters touch.
Word spacing
word-spacing adds space at each word separator. It also takes a length, and normal means no change. It is rarely needed, but it helps when letter spacing makes words look cramped, or when you want airy text in a short label.
.tagline {
letter-spacing: 0.05em;
word-spacing: 0.15em;
}Common errors
- Using a length for
line-heightonbody. Descendants with different font sizes inherit a fixed value that no longer fits. Use a unitless number. - Setting
letter-spacinginpx. The gap stays the same when the font size changes. Useem. - Fixing the height of a text box. If users increase line or letter spacing, the text overflows. Use
min-heightor let content size the box. - Applying letter spacing to lowercase paragraphs. It breaks the shape of words and slows reading.
- Expecting a trailing gap to be absent. Letter spacing is added after the last character too, which can make centered text look slightly off to the left.
Exercise
A page sets body { font-size: 1rem; line-height: 150%; }. A .note element inside has font-size: 2rem. What line height does .note get, and what change would make it scale with its own font size?
Show answer
With a 16px base, 150% computes to 24px on the body, and .note inherits that fixed 24px even though its font is 32px, so its lines would overlap. Changing the body to line-height: 1.5 makes .note compute 32px × 1.5 = 48px.
Exercise
You want an uppercase label in a 0.875rem font with spacing of about 0.08 times the font size between letters. Which unit should you use for letter-spacing, and why?
Show answer
Use em (letter-spacing: 0.08em), because em is relative to the element's font size, so the spacing scales when the font size changes.