HTML & CSSEasy
What is line-height?
line-height sets the height of each line box, which controls the vertical spacing between lines of text. The best practice is a unitless value such as 1.5, a multiplier of the element's own font size that children inherit as a ratio and recalculate for their own font size.
Value types
- Unitless (
1.5): recommended. Each descendant computes it from its font size. - Length (
24px,1.5rem): fixed; inherited as that exact length, which can make large headings overlap. - Percentage /
em(150%,1.5em): computed once on the parent and inherited as a fixed length, with the same overlap problem. normal: about 1.2, depending on the font.
Why unitless wins
body { font-size: 16px; line-height: 1.5em; } /* computes 24px, inherited as 24px */
h1 { font-size: 48px; } /* 48px text on 24px lines: overlaps */
With line-height: 1.5, the h1 gets 72px lines automatically.
Good defaults
- Body text: 1.5 to 1.7 (WCAG 1.4.12 requires layouts to survive at least 1.5).
- Headings: 1.1 to 1.3, because large text needs tighter leading.
- Buttons:
1or1.2, with vertical padding for height.
Related
- Half the extra space (leading) is added above and below the text, which is why text does not look perfectly centered in a box. The
text-box: trim-both cap alphabeticproperty (newer browsers) trims that extra space. lhandrlhunits equal one line height, useful for spacing.
Interview tip
Explain the unitless inheritance difference with the heading example; it is the classic follow-up.
Code example
html
1<style>2 body { font-family: system-ui; max-width: 380px; font-size: 16px; }3 .box { border: 2px solid #0b1020; padding: 8px; margin: 10px 0; }4 .tight { line-height: 1; }5 .comfy { line-height: 1.6; }6 .em-bug { line-height: 1.5em; } /* inherited as 24px */7 .unitless { line-height: 1.5; } /* inherited as a ratio */8 .em-bug h2, .unitless h2 { font-size: 36px; margin: 0; }9</style>10<p class="box tight">line-height: 1. Lines are cramped and harder to read when the paragraph wraps onto several lines.</p>11<p class="box comfy">line-height: 1.6. Comfortable spacing that is easy to read when the paragraph wraps onto several lines.</p>12<div class="box em-bug"><h2>1.5em: this big heading overlaps itself</h2></div>13<div class="box unitless"><h2>1.5 unitless: this heading scales correctly</h2></div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.