Sub topic 8
Line height, paragraph spacing and headings
This chapter assumes you know the box model, the rem and em units, and the spacing scale from the earlier chapters. You should also know the difference between padding and margin, and the flow rule (.flow > * + *) that spaces stacked siblings. Here you apply those ideas to text, where white space does most of its grouping work.
Three kinds of vertical space in text
A column of text contains three separate gaps, and each one signals a different relationship:
- Line spacing is the space between lines of the same paragraph. It is set with
line-height. - Paragraph spacing is the space between two paragraphs. It is set with margins (or
gap) on the paragraphs. - Heading spacing is the space around a heading. It must attach the heading to the content below it and separate it from the content above it.
The proximity principle says that closer means related. For text, that gives a strict ordering: lines in one paragraph are closest, paragraphs in one section come next, and the space before a new section is largest. The space between a heading and its own first paragraph is smaller than the space between paragraphs.
If two of these gaps are equal, two levels of the structure merge. Equal line and paragraph spacing makes paragraphs run together. Equal space above and below a heading leaves it floating between two blocks, with no clear owner.
The diagram below shows the stack of gaps for one section.
Line height
line-height sets the height of each line box. Extra height is split evenly above and below the text, so a line height of 1.5 on 16px text gives a 24px line box with about 4px of added space on each side of the glyphs.
Always write line-height as a unitless number. A unitless value is inherited as the multiplier, so each descendant computes its own line height from its own font size. A value with a unit, such as 1.5em or 24px, is computed once on the element where it is declared, and descendants inherit the resulting length.
/* Problem: children inherit a computed length */
.bad {
font-size: 1rem;
line-height: 1.5em; /* computed as 24px, then inherited */
}
.bad .small {
font-size: 0.75rem; /* 12px text inside a 24px line: loose, 2x */
}
/* Fix: children inherit the multiplier */
.good {
font-size: 1rem;
line-height: 1.5;
}
.good .small {
font-size: 0.75rem; /* 12px text inside an 18px line: still 1.5 */
}Good defaults depend on the text:
| Text | Typical line height | Reason |
|---|---|---|
| Body text, 45 to 75 characters per line | 1.4 to 1.6 | Gives the eye room to find the start of the next line |
| Very long lines (over 80 characters) | 1.6 or more | The eye has farther to travel back, so it needs more help |
| Small print and captions | 1.4 to 1.5 | Small text feels cramped when tight |
| Large headings | 1.1 to 1.3 | At large sizes, 1.5 leaves visible holes between wrapped lines |
Headings are the usual mistake. If you set line-height: 1.5 on body and never override it, a two-line h1 at 3rem has about 24px of extra space between lines. That gap approaches the spacing between paragraphs and makes the heading look like two separate items.
body {
font-size: 1rem;
line-height: 1.5;
}
h1, h2, h3 {
line-height: 1.2;
}
.prose {
max-width: 65ch;
}The max-width: 65ch line limits the measure (line length). Line length and line height are linked, and fixing one often fixes the other.
Paragraph spacing
The gap between paragraphs must be clearly larger than the gap between lines. With 1.5 line height, the white space between two lines inside a paragraph is roughly 0.5em. A paragraph gap of 1em adds to the same half-leading, so the space between paragraphs is about three times the space between lines. That ratio is enough to see the break without effort.
Do not use <br><br> to create paragraph gaps. It carries no meaning for assistive technology, and the gap cannot be controlled by your scale. Use <p> elements and set their spacing in CSS.
Use one signal for a paragraph break. Either add space between paragraphs or indent the first line, but not both. Web pages almost always use space.
The flow rule from the previous chapter is the best tool here, because it adds space only between siblings. There is no stray margin after the last paragraph, and no margin at the top of the first one.
.prose > * {
margin: 0; /* remove browser default margins first */
}
.prose > * + * {
margin-block-start: 1em;
}Use em here instead of a token such as var(--space-3). An em margin is relative to the element's own font size, so a paragraph in .small text gets a smaller gap automatically. Use scale tokens for layout-level gaps (between cards, sections and form groups). Use em inside running text, where spacing should follow the type size. You can still pick values from the same ratios, such as 0.5em, 1em and 1.5em.
The reset line must come before the flow rule. Both selectors have the same specificity, so the later one wins. Without the reset, browser defaults like margin-block: 1em on p would add to or collapse with your own margins and give results you did not choose.
Headings: more space above than below
A heading introduces what follows it. So the space above a heading should be larger than the space below it. A good starting ratio is about 3:1 or 4:1, measured against the heading's own font size.
The flow rule handles this with two extra selectors:
.prose > * {
margin: 0;
}
.prose > * + * {
margin-block-start: 1em;
}
/* Space above a heading: it starts a new group */
.prose > * + h2 {
margin-block-start: 1.5em;
}
.prose > * + h3 {
margin-block-start: 1.25em;
}
/* Space below a heading: pulls the next block close */
.prose > h2 + *,
.prose > h3 + * {
margin-block-start: 0.5em;
}Check the specificity. .prose > * + h2 and .prose > h2 + * both score one class and one element, so source order decides when they both match. That only happens when an h2 directly follows another h2 (an empty section), and the heading-below rule wins there. If that case matters to you, add a specific rule after both.
The two margins are measured in different units of text. The 1.5em above an h2 uses the h2 font size. If the h2 is 1.75rem (28px), the gap above is 42px. The 0.5em below uses the following element's font size, a 16px paragraph, so the gap below is 8px. The gaps are 42px, 16px and 8px: section break, paragraph gap, heading to content. Each level is visibly different from the next.
Larger headings need more space above because the larger font size fills more of the gap visually. That is why the values are in em and not fixed lengths. Changing the heading size later keeps the proportions.
A heading that sits alone at the bottom of a page or column, away from its text, breaks the grouping. In printed output and multi-column layouts, add break-after: avoid to headings so the browser keeps them with the next block.
h1, h2, h3 {
break-after: avoid;
}A complete example
This page uses the rules above. Save it and open it in a browser. Then change .prose > h2 + * to margin-block-start: 1.5em and see how the heading detaches from its paragraph.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Text spacing demo</title>
<style>
body {
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
margin: 0;
padding: 2.5rem 1rem;
}
h1, h2, h3 {
line-height: 1.2;
}
.prose {
max-width: 65ch;
margin-inline: auto;
}
.prose > * { margin: 0; }
.prose > * + * { margin-block-start: 1em; }
.prose > * + h2 { margin-block-start: 1.5em; }
.prose > h2 + * { margin-block-start: 0.5em; }
</style>
</head>
<body>
<main class="prose">
<h1>Shipping policy</h1>
<p>We ship orders within two working days. Delivery times depend on your region and the carrier you pick at checkout.</p>
<p>Orders placed on weekends are processed on the next working day.</p>
<h2>Returns</h2>
<p>You can return unused items within 30 days of delivery. Start a return from your account page.</p>
<h2>Damaged items</h2>
<p>Photograph the damage and contact support within seven days.</p>
</main>
</body>
</html>Common errors
- Equal margins above and below headings. The heading floats between blocks. Make the space above larger than the space below.
- A fixed pixel or
emline height onbody. Child elements inherit a length, not a ratio, and small or large text looks too loose or too tight. - Inheriting body line height into large headings. Override it with 1.1 to 1.3.
- Setting both
margin-bottomon headings andmargin-topon the next block. Adjacent vertical margins collapse, so only the larger one applies. You may think you are adding 0.5em to 1em, but you get 1em. Set the space in one place, as the flow rule does. - Stray space at the end of a container. A
margin-bottomon every paragraph leaves a gap after the last one. Use the flow rule, which only adds space between siblings. - Paragraph gap that is too small. If it is about the same as the white space between lines, readers cannot see where paragraphs end.
Exercise
A stylesheet sets h2 { margin-top: 1rem; margin-bottom: 2rem; } and p { margin-bottom: 1rem; }. The headings look detached from the paragraphs under them and attached to the paragraphs above them. Explain why, and rewrite the rules so each heading groups with the content below it using the flow-rule approach.
Show answer
The heading has 2rem of space below it and only 1rem above it, so it sits closer to the previous paragraph than to its own content. The ratio is the wrong way round. Remove the margins and use the flow rule, for example: .prose > * { margin: 0; } .prose > * + * { margin-block-start: 1em; } .prose > * + h2 { margin-block-start: 1.5em; } .prose > h2 + * { margin-block-start: 0.5em; }. Now the space above each heading is larger than the space below it. Also, the collapsed margin between a heading and the next paragraph in the original (2rem versus 1rem) would have given only 2rem, not 3rem.
Exercise
A page has body { line-height: 24px; } and a caption styled font-size: 0.75rem. The caption's lines look widely spaced. What is happening, and what is the fix?
Show answer
A line height with a unit is inherited as a fixed length, so the 12px caption text gets 24px lines (a ratio of 2). Change body to a unitless line-height: 1.5. The caption then computes 1.5 × 12px = 18px.