Sub topic 2
Closer means related: the proximity principle
This chapter assumes you can read basic HTML and CSS, know the box model (content, padding, border, margin), and are comfortable with rem units. It covers why distance carries meaning and how to turn that into spacing decisions. Later chapters cover the tools in detail: padding versus margin, spacing scales, gap, and margin collapsing.
The principle
The proximity principle comes from Gestalt psychology, the study of how people perceive patterns. It says that elements placed close together are seen as a group, and elements placed far apart are seen as separate.
This happens before the reader consciously reads anything. Within a fraction of a second, the eye sorts a page into clusters. Spacing therefore works as a signal. Every gap you write in CSS tells the reader either "these belong together" or "this is something new".
The signal comes from comparison. A 24px gap is not "large" or "small" in itself. It is large next to 8px gaps and small next to 64px gaps. The reader judges each gap against its neighbours, so the ratio between gaps matters more than any single value.
Why uniform spacing fails
The most common spacing mistake is giving every element the same gap. If a heading, the paragraph below it, and the next section's heading are all 24px apart, nothing is grouped. The heading looks as related to the paragraph above it as to the paragraph it introduces. The reader has to read the text to work out the structure, and spacing should have shown it.
Compare two versions of the same content in one file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Proximity demo</title>
<style>
body {
font: 1rem/1.5 system-ui, sans-serif;
max-width: 36rem;
margin: 2rem auto;
padding: 0 1rem;
}
h2 { font-size: 1.25rem; }
/* Version A: every gap is the same */
.uniform h2,
.uniform p {
margin: 1.5rem 0;
}
/* Version B: small gap inside a group, large gap between groups */
.grouped h2 { margin: 2.5rem 0 0.5rem; }
.grouped p { margin: 0 0 1rem; }
hr { margin: 3rem 0; }
</style>
</head>
<body>
<div class="uniform">
<h2>Delivery</h2>
<p>Orders ship within two working days.</p>
<p>Tracking details arrive by email.</p>
<h2>Returns</h2>
<p>You can return items within 30 days.</p>
<p>Refunds take five working days.</p>
</div>
<hr>
<div class="grouped">
<h2>Delivery</h2>
<p>Orders ship within two working days.</p>
<p>Tracking details arrive by email.</p>
<h2>Returns</h2>
<p>You can return items within 30 days.</p>
<p>Refunds take five working days.</p>
</div>
</body>
</html>In version A, the "Returns" heading floats halfway between the shipping paragraph and its own text. In version B, the gap above a heading (2.5rem) is much larger than the gap below it (0.5rem). The heading attaches to its own content, and the two sections read as two blocks.
Where a heading's top margin meets the previous paragraph's bottom margin, the browser uses the larger of the two instead of adding them. That is why version B has a 2.5rem gap before "Returns" and not 3.5rem. This behaviour is called margin collapsing, and a later chapter covers it. For now, read the values as "the biggest one wins".
Deciding which gap to use
For each element, ask what it belongs to. The answer decides which gap goes on each side.
A subheading is the classic case of the "belongs with the element below" branch. A closing paragraph at the end of a section takes the "belongs with the element above" branch for its top gap, and the section break comes after it.
Nested groups need more than two levels
Real pages have groups inside groups. A product card contains a title and a price, which form a pair, and the card sits in a list of cards, which sits in a section. If you have only two gap sizes, one of those relationships will look wrong.
Aim for one distinct gap size per level of relationship, and make each level clearly larger than the one inside it. A workable set for a content page:
| Relationship | Example | Relative gap |
|---|---|---|
| Tightly bound pair | Heading and its first paragraph; label and input | Smallest |
| Siblings in the same group | Consecutive paragraphs; items in a list | Small |
| Separate groups in one section | Two cards; two subsections | Medium |
| Separate sections | Features block versus pricing block | Large |
The exact values do not matter much here. The next chapter shows how to fix a small set of values so you apply them consistently. What matters is that the steps are visibly different.
How big should the difference be?
There is no exact threshold, but a gap that is only 10 to 20 percent larger than its neighbour is hard to see. Gaps of 16px and 20px read as the same gap with a rendering error. A ratio of about 1.5 to 2 between neighbouring levels is a practical starting point. Check it by looking at the page, not by trusting the numbers.
Proximity works without borders
Because spacing carries the grouping, you can often remove other visual devices. Boxes, rules and background colours also group content, but each adds visual noise. When you notice you are adding a border to make two things look separate, check first whether a larger gap would do it.
This does not make borders wrong. Use them when the content needs a strong boundary, such as a card the user can click as a whole, or a table where rows and columns must align. But spacing is the cheaper tool, and it does not compete with the text for attention.
/* Group boundary using a border: heavy */
.notice {
border: 1px solid #ccc;
padding: 1rem;
}
/* Group boundary using space only: lighter */
.notice-plain {
margin-block: 3rem;
}Proximity combined with other cues
Spacing is the strongest everyday grouping cue, but it is not the only one. Similarity (same colour, same type style) and common regions (a shared background) also group things. Problems appear when these cues disagree.
- If two items look identical but sit far apart, they read as separate. Spacing overrides similarity at this scale.
- If an item is next to a group but styled differently, it reads as an add-on, which may be what you want for a note or a caption.
- If a background box contains items that are spaced widely apart, the box says "group" and the spacing says "not a group". The reader has to resolve the conflict. Tighten the inside spacing or remove the box.
When the cues agree, the structure is clear without effort. When they disagree, readers slow down.
Common errors
These patterns come up repeatedly in code reviews.
- Equal spacing everywhere. Every element has
margin: 1rem 0. Nothing is grouped. - A heading centred between its neighbours. The heading has equal top and bottom margins. Give it a larger top margin than bottom margin.
- A caption nearer the wrong image. In a stack of figures, the caption sits closer to the next image than to its own. Put the caption's gap to its image at the smallest level.
- Gaps that differ by a few pixels. Values like 14px, 16px and 18px used for different relationships look accidental, so the reader sees no structure.
- Inside gaps larger than outside gaps. Items within a list are spaced further apart than the list is from the paragraph that follows. The list then dissolves into the page.
- An orphaned action. A "Save" button sits equally far from two unrelated fieldsets, so it is unclear what it saves. Place it with the group it acts on, or give it clear separation if it acts on everything.
- Fixing grouping with margins on both sides. Putting
margin: 2rem 0on every block, then compensating on a few elements, leads to overrides that are hard to maintain. Choose a rule for the default gap, then add exceptions on the few elements that start a new group.
A quick way to check
Before moving on, you can test any layout with a rough version of the later squint test. Blur your eyes, or step back from the screen, until the text is unreadable. The page should still show distinct blocks. If you cannot tell where one group ends and the next begins, the gaps are too similar. A later chapter turns this into a full audit method.
Summary of the working model
- Distance is read relatively, so compare gaps to their neighbours.
- Each level of relationship gets its own gap, and each is clearly larger than the one inside it.
- Headings and labels belong to what follows them, so the larger gap goes above.
- Spacing can replace borders and boxes for grouping, and when it is combined with them, all the cues must agree.
Exercise
A page has an h3 followed by a paragraph. Both the h3 and the paragraph have margin: 1rem 0, and the next h3 starts right after the paragraph. Describe the grouping problem and write CSS that fixes it.
Show answer
The h3 is the same distance from the paragraph above it and the paragraph below it, so it does not visibly attach to its own text. Margins also collapse between neighbours, so every gap is 1rem. A fix is h3 { margin: 2rem 0 0.5rem; } p { margin: 0 0 1rem; }. The heading now sits 0.5rem from its paragraph and 2rem from the previous block, so it clearly belongs to the text below it.
Exercise
A card has a title, a price and a "Buy" button. In the current CSS all three are 1rem apart, and the cards in the list are also 1rem apart. Why is this a problem, and what would you change?
Show answer
The gap inside a card equals the gap between cards, so the reader cannot tell where one card ends. Reduce the gaps inside each card (for example 0.25rem between title and price, 0.75rem before the button) and increase the gap between cards (for example 2rem). The gap between cards must be clearly larger than any gap inside a card.