Sub topic 10
Spacing and Proximity: Tying Headings to Their Text
This chapter assumes you know the basic vocabulary of type (typeface, size, weight) and can read simple CSS. You have already seen how size, a type scale, and line height rank text. Here you will use space to show which pieces of text belong together.
Why proximity does the grouping
Readers group things that sit close together. Designers call this the principle of proximity. When two blocks of text are near each other, the eye treats them as one unit. When they are far apart, the eye treats them as separate units.
A heading has one job in relation to the text below it: to introduce that text. If the gap above the heading is the same as the gap below it, the heading floats between two paragraphs and the reader cannot tell which one it belongs to. The fix is not a bigger font or a bolder weight. The fix is to move the heading closer to the text it introduces and farther from the text it follows.
The rule in one line: space above a heading should be larger than space below it.
Space above versus space below
Think of the page as a series of sections. Each section has a heading and one or more paragraphs. The space between sections should be clearly larger than the space inside a section.
| Gap | Relationship it signals | Typical size |
|---|---|---|
| Between two paragraphs in one section | Same topic, continuing | 0.75em to 1em |
| Between a heading and its first paragraph | Introduction and content, tightly bound | 0.3em to 0.6em |
| Between the last paragraph and the next heading | New section begins | 1.75em to 2.5em |
Three levels of gap are enough for most layouts. The inside-section gaps stay small, and the between-section gap is roughly three to four times the heading-to-text gap.
The following stylesheet applies this rule. The heading has a larger top margin than bottom margin, so it pulls toward the paragraph that follows it.
h2 {
font-size: 1.563rem;
font-weight: 600;
margin-top: 2em;
margin-bottom: 0.5em;
}
h2 + p {
margin-top: 0;
}The selector h2 + p is the adjacent sibling combinator. It matches a paragraph that comes directly after an h2. Setting its top margin to 0 stops the paragraph from adding extra space on top of the heading's bottom margin. Without this line, the paragraph's own margin could widen the gap and weaken the bond.
Margins use em units for a reason
Notice that the margins above are in em, not rem or pixels. One em equals the font size of the element itself. A heading with font-size: 1.563rem and margin-top: 2em gets a top margin of about 50 pixels at a 16-pixel root size. A smaller h3 with the same 2em gets a smaller margin.
This means the spacing scales with each heading level automatically. Larger headings get larger gaps, which matches the larger visual weight they carry. If you change the type scale later, the spacing follows without further edits.
Margin collapse and other surprises
Vertical margins in CSS behave in a way that surprises many people. When the bottom margin of one block meets the top margin of the next, the two margins collapse into one. The result is the larger of the two, not the sum.
Suppose a paragraph has margin-bottom: 1em and the next h2 has margin-top: 2em. The gap between them is 2em, not 3em. This is useful, because it keeps your spacing predictable. It also explains a common error: you add a margin to a paragraph and nothing changes, because the neighboring margin was already larger.
Collapse does not happen in every case. It is blocked inside flex and grid containers, and it does not apply when the parent has padding or a border between the two margins. If your spacing looks different inside a flex layout than in plain flow, this is the likely cause.
.card {
display: flex;
flex-direction: column;
}
.card h2 {
margin-top: 1.5em;
margin-bottom: 0.4em;
}
.card p {
margin-top: 0;
margin-bottom: 1em;
}In the flex column above, margins do not collapse. Each gap is the plain sum of the neighboring margins. Setting margin-top: 0 on the paragraph keeps the heading-to-text gap at exactly 0.4em of the heading's size.
A diagram of the spacing logic
The diagram below shows how the three gaps stack on a page with two sections.
Read the arrows as distances. The gap before a heading is the large one. Every other gap is smaller, and the smallest sits directly under a heading.
Leading and spacing work together
Line height (leading) sets the space between lines inside a paragraph. Margins set the space between blocks. The two have to be in proportion, or the page looks uneven.
With body text at line-height: 1.5, each line takes 1.5em of height. A paragraph gap of 1em then reads as slightly less than one full blank line, which is enough to separate paragraphs without opening a hole in the page. If you set the paragraph gap to 0.2em, paragraphs blur together. If you set it to 3em, the page falls apart into disconnected pieces.
Large headlines need a different treatment. A headline has fewer lines and a bigger size, so tight leading (about 1.15) looks better. Tight leading means a multi-line headline also takes less vertical room, so the space below it can stay small.
h1 {
font-size: 2.441rem;
line-height: 1.15;
font-weight: 700;
margin-top: 0;
margin-bottom: 0.4em;
}
.lead {
font-size: 1.25rem;
line-height: 1.4;
margin-top: 0;
margin-bottom: 2em;
}Here the headline sits close to its lead paragraph, and a large gap follows the lead, which separates the introduction from the first section.
A vertical rhythm option
Some designers pick a base unit and make every gap a multiple of it. With a 1.5 line height on 16-pixel text, the unit is 24 pixels. Gaps then use 12, 24, 48 pixels and so on. This gives the page a steady rhythm and makes the grouping obvious.
Strict rhythm has a cost. Headings with different line heights rarely fit the grid exactly, and forcing them to fit can produce awkward gaps. For most pages, a small set of consistent multiples is enough. Treat the unit as a guide, not a law.
Common errors
- Equal gaps above and below a heading. The heading looks detached. Raise the top margin or lower the bottom margin.
- Too small a gap between sections. The page reads as one long block. Increase the space above each heading.
- Too large a gap below a heading. The heading looks like a caption for the previous paragraph, or like a stray line. Keep it within about half an em.
- Stacked headings. An
h2followed directly by anh3should have a small gap between them, not two full section gaps. Add a rule such ash2 + h3 { margin-top: 0.5em; }. - Forgetting collapse. Adding margin to one element has no effect when its neighbor has a larger margin. Check which margin is winning in your browser's developer tools.
- Using empty paragraphs or line breaks for space. These break the semantics and cannot scale with the type. Use margin or padding.
Checking the result
Squint at the page or blur it. You should see clusters: each heading attached to its text, with clear channels between clusters. If two clusters merge, widen the gap between them. If a heading drifts toward the wrong cluster, shrink the gap below it or enlarge the gap above it.
You can also check the semantics. Real heading elements (h1, h2, h3) let CSS apply these rules by level, and assistive technology can navigate by them. Spacing is a visual cue. The markup should express the same structure.
<article>
<h1>Headline states the main idea</h1>
<p class="lead">A short intro sentence in slightly larger text.</p>
<h2>Subhead introduces a section</h2>
<p>Body text carries the detail.</p>
<h2>Another subhead</h2>
<p>More detail for the second section.</p>
</article>Exercise
A page has h2 { margin-top: 1em; margin-bottom: 1em; } and p { margin: 0 0 1em; }. Readers say they cannot tell which paragraph each subhead belongs to. What two changes fix it, and what is the gap between a paragraph and the following h2 after your changes if you set the h2 margins to 2em above and 0.5em below?
Show answer
Make the space above the subhead larger than the space below it, and remove any extra top margin on the paragraph that follows the subhead (h2 + p { margin-top: 0; }). With h2 { margin-top: 2em; margin-bottom: 0.5em; }, the paragraph's bottom margin (1em) collapses with the heading's top margin (2em), so the gap is the larger value, 2em of the heading's font size, not 3em.
Exercise
Inside a flex column container, a paragraph has margin-bottom: 1em and the next heading has margin-top: 2em. How large is the gap, and why is it different from the same elements in normal flow?
Show answer
The gap is 3em in the sum of the two margins' sizes (measured in each element's own em). Margins do not collapse inside flex or grid containers, so they add up. In normal block flow they would collapse to the larger value, 2em.