Sub topic 8
Line height, paragraph spacing and headings
This chapter assumes you know the CSS box model, rem and em units, and the spacing scale from the earlier chapter. Here you apply that scale to text, so that paragraphs, headings and lists read as clear groups.
Three spaces inside a block of text
Text has three kinds of white space, and each one groups content at a different level.
| Space | CSS property | What it groups |
|---|---|---|
| Between lines | `line-height` | Lines into one paragraph |
| Between paragraphs | `margin` or a flow rule | Paragraphs into one section |
| Around headings | `margin` | A heading with the text it introduces |
The proximity principle applies to all three. The gap between lines must be smaller than the gap between paragraphs. The gap between paragraphs must be smaller than the gap above a heading.
Line height
line-height sets the height of each line box. Use a unitless number, such as 1.5. A unitless value is multiplied by the element's own font size, so children with different font sizes get proportional spacing. A value with a unit, such as 1.5rem, is inherited as a fixed length. A large heading inside that element would then have lines that overlap.
Body text usually reads well between 1.4 and 1.6. Large headings need less, often 1.1 to 1.3, because big type looks loose when the ratio stays the same.
body {
line-height: 1.5;
}
h1,
h2,
h3 {
line-height: 1.2;
}Paragraph spacing
If the space between paragraphs equals the line gap, readers cannot see where one paragraph ends. A common choice is one blank line, about 1em to 1.5em. Using em ties the gap to the font size of the text.
Put the space on one side only. The flow rule from the previous chapters does this and avoids doubled spacing:
.prose > * + * {
margin-block-start: 1em;
}Headings belong to what follows
A heading introduces the content below it. If it sits midway between the previous paragraph and the next one, the reader cannot tell which section it belongs to. Give it more space above than below.
.prose h2 {
margin-block-start: var(--space-5);
margin-block-end: var(--space-2);
}
.prose h2 + * {
margin-block-start: 0;
}The first rule sets 2.5rem above the heading and 0.5rem below. The second rule removes the flow rule's margin from the element right after the heading, so the heading's own bottom margin decides the gap. Both values come from the scale, and the ratio is five to one.
Common errors
- Equal gaps everywhere. If headings have the same margin above and below, they look detached from their content. Make the upper gap clearly larger.
- Fixed line height on a parent.
line-height: 24pxonbodyis inherited as a length, so large headings get cramped lines. Use a unitless number. - Stacked margins. A heading's top margin and the previous paragraph's bottom margin collapse, as you saw earlier. Set spacing in one direction so the result is predictable.
- Gaps too close to the line height. If paragraph spacing is only a little bigger than the line gap, the paragraphs run together.
Try it
Copy this page into a file and open it in a browser. Change the heading's bottom margin to var(--space-5) and watch the heading drift away from its paragraph.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Prose spacing</title>
<style>
:root {
--space-2: 0.5rem;
--space-5: 2.5rem;
}
body { max-width: 40rem; margin: 2rem auto; line-height: 1.5; }
h2 { line-height: 1.2; }
.prose > * + * { margin-block-start: 1em; }
.prose h2 { margin-block-start: var(--space-5); margin-block-end: var(--space-2); }
.prose h2 + * { margin-block-start: 0; }
</style>
</head>
<body>
<div class="prose">
<p>First paragraph of the opening section, long enough to show how lines sit together when line height is set to one and a half.</p>
<p>Second paragraph in the same section, separated by one em.</p>
<h2>A new section</h2>
<p>This paragraph is close to its heading, so the two read as one group.</p>
</div>
</body>
</html>Exercise
A page sets body { line-height: 28px; }. An h1 at 3rem (48px) wraps onto two lines and the lines overlap. What is the cause, and what is the fix?
Show answer
The fixed 28px value is inherited as a length, so the 48px heading gets 28px lines, which is less than its font size. Change it to a unitless value such as line-height: 1.5 on body, and set a smaller one such as 1.2 on headings.
Exercise
An h2 has margin-top: 1.5rem and margin-bottom: 1.5rem. Readers say it is unclear which paragraph it introduces. Which change fixes this, and why?
Show answer
Reduce the bottom margin, for example to 0.5rem, and increase the top margin, for example to 2.5rem. By the proximity principle, the heading then sits closer to the text it introduces than to the text before it.