Sub topic 2
Closer means related: the proximity principle
This chapter assumes you know basic HTML structure, CSS selectors and the box model, and rem units. It explains the rule that decides how far apart to place things. The next chapters turn that rule into CSS tools.
The principle
The proximity principle comes from Gestalt psychology. It says people see elements that are close together as one group, and elements that are far apart as separate groups. Readers do this automatically, before they read any text.
So distance carries meaning. If you place two elements 4px apart, you tell the reader they belong together. If you place them 64px apart, you tell the reader they don't. Every gap on a page makes this claim, whether you meant it or not.
A heading that floats
The most common proximity error is a heading that sits midway between the text above it and the text below it. The reader can't tell which section it introduces.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Proximity demo</title>
<style>
body { max-width: 32rem; margin: 2rem auto; font: 1rem/1.5 system-ui, sans-serif; }
h2, p { margin: 0; }
.ambiguous h2 { margin-block: 1.5rem; }
.clear h2 { margin-block: 2.5rem 0.5rem; }
</style>
</head>
<body>
<section class="ambiguous">
<p>Orders are shipped within two days.</p>
<h2>Returns</h2>
<p>You can return items within 30 days.</p>
</section>
<section class="clear">
<p>Orders are shipped within two days.</p>
<h2>Returns</h2>
<p>You can return items within 30 days.</p>
</section>
</body>
</html>In .ambiguous, the heading has 1.5rem above and below, so it belongs to neither paragraph. In .clear, it has 2.5rem above and 0.5rem below. It now sits closer to the paragraph it introduces, and the paragraph before it reads as a separate group.
Use three levels of distance
For any group of elements, ask which relationships exist and rank them by strength. A stronger relationship gets a smaller gap.
| Relationship | Example | Relative gap |
|---|---|---|
| Tightly bound | A label and its input | Smallest |
| Same group | One field and the next field | Medium |
| Different groups | One form section and the next | Largest |
The gaps must differ enough for the eye to notice. A gap of 16px between fields and 20px between groups is too close, and the reader sees one long list. A common starting point is a ratio of about 1.5 to 2 between each level and the next.
Common errors
- Uniform spacing. Giving every element the same margin makes everything look equally related, so no groups appear.
- Spacing that contradicts the markup. If two paragraphs are in separate sections but sit 8px apart, the layout says one thing and the HTML says another.
- Gaps that are almost equal. Differences below roughly 1.5 times are hard to see, so the levels blur together.
- Compensating with borders. Boxes and rules around everything add visual noise. Try spacing first and add a border only if spacing alone can't carry the grouping.
Where this goes next
You now have the rule: closer means related, and gaps should increase as relationships weaken. Applying it by hand with arbitrary values is how pages end up with a dozen near-identical margins. The next chapters cover padding, margin and a fixed spacing scale, which let you apply these levels consistently.
Exercise
A product card has a title, a price directly under it, a description, and a "Buy" button. The title-to-price gap is 1rem, the price-to-description gap is 1rem, and the description-to-button gap is 1rem. Shoppers often read the price as part of the description. Using the proximity principle, how would you change the gaps?
Show answer
Make the title-to-price gap smaller (for example 0.25rem) so the two read as one unit, and keep a larger gap (for example 1rem) between the price and the description. The button can get a still larger gap (for example 1.5rem) to separate the action from the content. Equal gaps give no grouping signal, so unequal gaps that follow the relationships fix the problem.