Sub topic 9
Leading in CSS: unitless line-height
This chapter assumes you know basic HTML and CSS, understand font sizes in rem and px, and know that leading is the space between lines of text. In CSS, you set leading with the line-height property. The way you write its value decides whether it behaves well or causes bugs.
What line-height controls
line-height sets the height of each line box. The browser centers the text vertically inside that box, so the extra space is split between the top and bottom of every line. A font size of 16px with a line-height of 24px gives 8px of extra space per line, split as 4px above and 4px below.
You can write the value in several ways:
| Value | Example | What it means |
|---|---|---|
| Unitless number | `1.5` | 1.5 times the element's own font size |
| Length | `24px`, `1.5rem` | A fixed height |
| Percentage | `150%` | 150% of the element's own font size, computed once |
| Keyword | `normal` | The font's built-in default, often about 1.2 |
Why unitless is the safer choice
The difference shows up in inheritance. A unitless number is inherited as the number itself. Each child element multiplies it by its own font size. A length or percentage is computed on the element where you set it, and children inherit the finished pixel value.
Suppose body has font-size: 1rem (16px) and line-height: 150%. The computed value is 24px, and every descendant inherits 24px. A heading with font-size: 3rem (48px) then gets 24px lines. If the heading wraps to two lines, they overlap. With line-height: 1.5, the heading gets 72px lines and the body text stays at 24px.
body {
font-size: 1rem;
line-height: 1.5;
}
p {
max-width: 65ch;
}Here line-height: 1.5 is set once on body. Every element inherits the ratio and scales it by its own size. A small caption gets tighter lines in pixels, and a large heading gets looser ones, with no extra rules.
Adjusting leading for different elements
Set a sensible default on body, then override it only where the content calls for it. Headings are usually set in larger type on shorter lines, so they need a lower ratio than body text. Wide text columns need a little more.
body {
line-height: 1.5;
}
.article p {
max-width: 70ch;
line-height: 1.6;
}
.caption {
max-width: 45ch;
line-height: 1.4;
}
h1,
h2,
h3 {
line-height: 1.2;
}The article paragraph has a wider measure, so it gets 1.6. The caption has a narrow measure, so it gets 1.4. Headings use 1.2 because large type with a generous ratio looks loose.
Common errors
- Setting
line-heightwith a unit onbody. Children inherit a fixed length, which breaks headings and small text. - Using
line-height: 1on multi-line body text. Ascenders and descenders on neighboring lines nearly touch. - Putting
line-heighton a fixed-height container. If you also setheightin pixels, larger user text settings can clip the text. Prefermin-heightor let the content size the box. - Forgetting form controls. Some browsers apply their own
line-height: normaltoinputandbutton. Setline-height: inheriton them if you want them to match.
Exercises
Exercise
A page sets body { font-size: 16px; line-height: 150%; }. A heading inside has font-size: 40px. What is the line height of the heading, and what would it be if you wrote line-height: 1.5 instead?
Show answer
With 150%, the percentage is computed on body (16px × 1.5 = 24px) and the heading inherits 24px, which is smaller than its 40px font size, so wrapped lines overlap. With the unitless 1.5, the heading computes 40px × 1.5 = 60px.
Exercise
Write CSS that gives body text a line-height of 1.5, gives paragraphs inside .wide a line-height of 1.6, and makes button elements use the same line-height as their parent.
Show answer
body { line-height: 1.5; } .wide p { line-height: 1.6; } button { line-height: inherit; }