Sub topic 13
Line height, letter spacing, and word spacing
This chapter covers the three properties that control the empty space inside and around text: line-height, letter-spacing, and word-spacing. It assumes you know CSS selectors and inheritance, and that you have met em and rem units and font-size in earlier chapters. Small changes to these properties often change readability more than a change of font does.
How line height works
line-height sets the height of each line box in a block of text. A line box is the horizontal strip that holds one line of text. The browser takes the font's content area, which is roughly the glyph height, and adds equal extra space above and below. That extra space is called half-leading. If the font size is 16px and the line height is 24px, each line gets 4px of half-leading above and 4px below.
The initial value is normal. With normal, the browser uses the line spacing built into the font file. It is usually between 1.1 and 1.4 times the font size, and it differs from font to font.
| Value | Example | What it means |
|---|---|---|
| `normal` | `line-height: normal` | Use the font's built-in spacing |
| Number | `line-height: 1.5` | Multiply the element's own font size by 1.5 |
| Length | `line-height: 24px`, `1.5rem` | A fixed height |
| Percentage | `line-height: 150%` | A fraction of the element's own font size, computed once |
Unitless values and inheritance
line-height is an inherited property, and the form of the value changes what children inherit.
- A unitless number is inherited as the number itself. Each descendant multiplies it by its own font size.
- A length or percentage is converted to an absolute length on the element where it is declared. Descendants inherit that fixed length.
This difference causes a common bug. Suppose the body uses a percentage and a heading has a larger font:
body {
font-size: 16px;
line-height: 150%; /* computed once: 24px, then inherited as 24px */
}
h1 {
font-size: 48px; /* inherits 24px line height: lines overlap */
}A two-line h1 gets 24px lines for 48px text. The lines collide. With line-height: 1.5 on body, the heading would get 72px lines.
Use unitless values for line-height unless you want a fixed height. The spine example does this:
body {
font-family: system-ui, sans-serif;
line-height: 1.5;
}Choosing values
Good values depend on line length, font, and size.
- Body text: 1.4 to 1.6 works for most fonts. Longer lines need more space, because the eye has to travel farther to find the start of the next line.
- Headings: 1.1 to 1.3. Large text looks loose at 1.5, and headings are usually short.
- Small text such as captions: about 1.4 or slightly more, since small glyphs are harder to track.
- Fonts with a tall x-height: a large x-height is the height of lowercase letters like "x". Fonts with a tall one often need slightly more line height.
A reasonable set of defaults:
body {
line-height: 1.5;
}
h1,
h2,
h3 {
line-height: 1.2;
}
small,
figcaption {
line-height: 1.4;
}Edge cases
- Inline elements:
line-heighton an inline element such as<span>or<a>does not change the height of the line unless it is larger than the line's other content. The line box height is the tallest contribution on that line. - Mixed fonts on one line: if a fallback font or an inline icon has larger built-in metrics, the line can grow. Setting an explicit
line-heighton the parent usually keeps the lines steady. - Fixed-height containers: a length value such as
24pxcan make text clip or overlap when the user enlarges their font. Prefer unitless values and let the container grow. - Single-line centering: setting
line-heightequal to a button's height was a vertical centering trick. Padding or flexbox is safer, because wrapped text will overflow the fixed height.
Letter spacing
letter-spacing adds space between characters. Its initial value is normal, which lets the font decide. A length adds that amount after each character. Negative values pull letters together.
Use em units so the spacing scales with the text:
.eyebrow {
text-transform: uppercase;
font-size: 0.75rem;
letter-spacing: 0.08em;
}
.display-title {
font-size: clamp(2rem, 1rem + 4vw, 4rem);
letter-spacing: -0.02em;
}Points to keep in mind:
- Uppercase and small text benefit from positive spacing. Capitals were designed to be read next to lowercase, and set alone they look cramped. Small sizes also read more easily with a little extra room.
- Large display text often looks better with slight negative spacing. Keep it mild. Values below about
-0.05emrisk letters touching. - Avoid changing body text. Fonts are already spaced for reading at body sizes.
- Non-zero values disable ligatures in most browsers. A ligature is a single joined glyph for a pair like "fi". Browsers turn ligatures off when
letter-spacingis not zero, because joining letters would conflict with the spacing. The effect is rarely noticeable, but it explains why text can change shape when you add spacing. - Spacing is added after the last character too. In centered text, this shifts the visible text slightly to the left. For one-line labels, you can compensate with
padding-leftequal to the spacing, ormargin-rightset to the negative of it. - Cursive scripts break. In scripts like Arabic, where letters connect, adding letter spacing breaks the joins. Avoid it for those languages.
Word spacing
word-spacing changes the space added to each word separator. Its initial value is normal. A length is added to the normal space width, and negative values reduce it.
.wide-words {
word-spacing: 0.1em;
}It is rarely needed. It helps in two cases:
- Justified text: with
text-align: justify, the browser already stretches word gaps to fill each line. Narrow columns produce uneven "rivers" of white space. Reducing the problem needs better hyphenation or a wider column, notword-spacing, but a smallword-spacingcan adjust the baseline gap. - Display type with tight letter spacing: if you tighten
letter-spacingon a heading, word gaps can look relatively wide. A small negativeword-spacingevens them out.
User overrides and WCAG
WCAG success criterion 1.4.12, Text Spacing, requires that content stays usable when a user applies their own spacing, for example through a browser extension. The values it tests are:
| Property | User value |
|---|---|
| Line height | at least 1.5 times the font size |
| Paragraph spacing | at least 2 times the font size |
| Letter spacing | at least 0.12 times the font size |
| Word spacing | at least 0.16 times the font size |
You do not have to use these values yourself. You must make sure that nothing is cut off or overlapping when someone else applies them. The usual causes of failure are fixed heights, overflow: hidden, and line-height set in px. Use min-height instead of height, and let text containers grow.
Putting it together
This stylesheet combines the three properties with relative units, so it follows the user's font settings:
:root {
--measure: 65ch;
}
body {
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
p {
max-width: var(--measure);
margin-block: 0 1em;
}
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
line-height: 1.15;
letter-spacing: -0.01em;
}
.label {
font-size: 0.8125rem;
text-transform: uppercase;
letter-spacing: 0.06em;
word-spacing: 0.05em;
}Common mistakes
- Declaring
line-heightas a percentage onbody, then finding headings with overlapping lines. - Setting a fixed pixel
line-heighton text that users may enlarge. - Adding
letter-spacinginpx, which does not scale when the font size changes. - Adding positive letter spacing to lowercase paragraphs, which slows reading.
- Forgetting that centered, letter-spaced text is offset by the trailing space.
Exercise
A page sets body { font-size: 16px; line-height: 150%; } and h2 { font-size: 40px; }. What line height does a wrapped h2 get, and what single change to the body rule fixes the problem?
Show answer
The h2 inherits a computed line height of 24px (150% of 16px), so its lines are 24px apart for 40px text and overlap. Change the body rule to line-height: 1.5. The number is inherited as a multiplier, so the h2 computes 1.5 × 40px = 60px.
Exercise
You want an uppercase 0.75rem label to have spacing that stays proportional if its font size changes. Write the letter-spacing declaration, and say why px is a poor choice.
Show answer
Use letter-spacing: 0.08em; (a value near 0.05em to 0.1em is fine). An em value is relative to the element's font size, so the spacing grows and shrinks with the text. A px value stays fixed, so it would look too wide when the text gets smaller and too tight when it gets larger.