Sub topic 9
Leading in CSS: unitless line-height
This chapter assumes you know what leading is and the commonly cited ranges for it. It also assumes you can write basic CSS selectors and declarations. Here you will learn how the line-height property works, why the unitless form is the standard choice, and which problems it causes or prevents.
What line-height actually controls
In CSS, leading is set with the line-height property. It sets the minimum height of each line box. A line box is the horizontal strip that holds one line of text.
The browser does not add the extra space only below the line. It splits it. Each glyph has a content area based on the font's own metrics. The browser takes the difference between the line-height and that content area and divides it in half. One half goes above the text and the other half goes below. This is called half-leading.
For a 16px font with line-height: 1.5, the line box is 24px tall. The text sits in the middle of that box, with about 4px of extra space on each side, depending on the font's metrics.
Two consequences follow:
- The first line of a paragraph has half-leading above it. This is why text in a box with
padding: 0still looks slightly inset from the top. - Changing
line-heightshifts the text's vertical position inside its box, not only the space between lines.
The four kinds of values
line-height accepts several kinds of values. They look similar but behave differently.
| Value | Example | What children inherit |
|---|---|---|
| `normal` | `line-height: normal` | The keyword. Each element uses its own font's metrics, usually about 1.1 to 1.3 times the font size. |
| Number (unitless) | `line-height: 1.5` | The number itself. Each child multiplies it by its own font size. |
| Length | `line-height: 24px` or `1.5em` | The computed length. Children get the same fixed height whatever their font size. |
| Percentage | `line-height: 150%` | The computed length, the same as a length. |
The difference in the last two rows is the reason this chapter exists. 1.5em and 150% look like "1.5 times the font size", but the browser converts them to a pixel length on the element where they are written. Children inherit that pixel length, not the ratio.
Why unitless wins
Take a page where the body text is 16px and headings are larger.
body {
font-size: 1rem;
line-height: 150%;
}
h2 {
font-size: 2rem;
}The body computes 150% of 16px, which is 24px. The h2 inherits 24px, not the percentage. A 32px heading with a 24px line height is shorter than its own text. When the heading wraps to two lines, the lines overlap or touch.
Now use the unitless form:
body {
font-size: 1rem;
line-height: 1.5;
}
h2 {
font-size: 2rem;
}The h2 inherits the number 1.5 and multiplies it by its own 32px font size. Its line height becomes 48px. Every element ends up with the same proportion between line height and type size, whatever its font size is. This also keeps leading correct when a user changes the browser's default font size, or when you change a font size in a media query later.
A sensible baseline
A minimal reading setup combines unitless leading with a measure limit. Here the ch unit counts the width of the "0" glyph, so 65ch is roughly 65 characters per line. The next chapter covers it in detail.
body {
font-size: 1rem;
line-height: 1.5;
}
p {
max-width: 65ch;
}A value of 1.5 is a common starting point for body text at a moderate measure. It is not a law. The earlier chapters explain why a typeface with a large x-height or a long measure pushes the number up, and why a short measure can bring it down.
Overriding by context
Because the inherited value is a ratio, you can set it once on body and override it only where the context changes. Leading and measure move together, so a wider article column gets slightly more leading and a narrow caption gets slightly less.
body {
line-height: 1.5;
}
.article p {
max-width: 70ch;
line-height: 1.6;
}
.caption {
max-width: 45ch;
line-height: 1.4;
}Headings are the usual exception. Large display text has proportionally more space between lines than it needs, so give headings a smaller ratio:
h1,
h2,
h3 {
line-height: 1.2;
}A heading at 2rem with a ratio of 1.2 has a 38.4px line height. That is tight but comfortable for one or two lines. Using the body ratio of 1.5 would leave a gap that makes a two-line heading look like two separate items.
Setting leading in a design token
If you keep your sizes in custom properties, store the ratio as a bare number so it stays unitless:
:root {
--leading-body: 1.5;
--leading-heading: 1.2;
}
body {
line-height: var(--leading-body);
}
h1,
h2,
h3 {
line-height: var(--leading-heading);
}A custom property holds the value as written, so --leading-body: 1.5 stays a number when it is used. Writing --leading-body: 150% would turn it back into a percentage and bring back the inheritance problem.
Pitfalls
Fixed heights
If you set height: 3rem on a container and then enlarge the text or the line height, the text overflows. Prefer min-height, or let the content decide the height. This matters again in the accessibility chapter, where user style overrides raise the line height.
Inline elements that change line height
A line box grows to fit its tallest inline box. If a span inside a paragraph has a larger font size and inherits the ratio, it can push that one line taller than its neighbors. Superscripts and subscripts are the classic case, because they raise or lower the baseline and stretch the line. The usual fix is:
sup,
sub {
line-height: 0;
}With a line height of 0, the element adds no height to the line box, but the text still renders.
Form controls and buttons
Browsers give many form controls their own line-height: normal, and they do not always inherit from the body. If a button or input looks different from your text, set it explicitly:
button,
input,
select,
textarea {
font: inherit;
line-height: inherit;
}The font shorthand resets line-height too, so font: inherit alone may already do the job. The explicit line-height: inherit documents the intent.
Mixing unitless and length values in a shorthand
In the font shorthand, the line height follows the size after a slash: font: 1rem/1.5 Georgia, serif;. A unitless number there stays unitless. Writing font: 1rem/150% Georgia, serif gives the percentage behavior. Also remember that the font shorthand resets line-height to normal if you omit it, so a later font: 1rem Georgia can silently remove the leading you set above.
Fractional pixel results
A ratio like 1.6 on a 15px font gives 24px, but 1.4 on 17px gives 23.8px. Browsers handle fractional line heights, though text can land on slightly different pixel positions between lines on some screens. This rarely matters for reading. If you need exact alignment, choose ratios that produce whole numbers for your base size.
Quick reference
- Set
line-heightonce onbodyas a unitless number. - Override with a number on specific components such as articles, captions, and headings.
- Avoid
em,%, andpxfor line height unless you want a fixed height on purpose. - Keep ratios in custom properties as bare numbers.
- Give headings a lower ratio than body text.
- Never fix the height of a block that contains text.
Exercise
A page sets body { font-size: 16px; line-height: 150%; } and h2 { font-size: 40px; }. What line height does the h2 get, and what would it be if the body used line-height: 1.5 instead?
Show answer
With 150%, the body computes 24px and the h2 inherits 24px, which is shorter than its 40px text, so wrapped lines crowd or overlap. With the unitless 1.5, the h2 inherits the number and multiplies it by its own size: 1.5 × 40px = 60px.
Exercise
You store --leading: 150%; in :root and use line-height: var(--leading) on body. Headings later look cramped. What went wrong, and how do you fix it?
Show answer
The custom property holds a percentage, so the body computes a pixel length and children inherit that length instead of the ratio. Change the token to the bare number --leading: 1.5; so each element multiplies it by its own font size.
Exercise
A paragraph contains a sup element for a footnote, and the line holding it is taller than the other lines. Which declaration stops this, and why does it work?
Show answer
sup { line-height: 0; }. The raised, inherited-ratio inline box was stretching the line box. With a line height of 0 the element contributes no height to the line box, while its text still renders.