Sub topic 8
Spacing and Proximity: Grouping Related Content
This chapter assumes you know basic HTML and CSS, including custom properties and the box model. It also assumes you have read the earlier chapters on size, contrast, and position. Those cues set which element is seen first. Spacing decides which elements the reader sees as belonging together.
The proximity principle
The proximity principle says that items placed close together look related, and items placed far apart look separate. Readers apply this rule before they read a single word. Whitespace (the empty area around elements) is therefore a grouping tool, not only decoration.
Consider a form. If every label and input sits the same distance from the next one, nothing tells the reader which label belongs to which input. If each label is close to its own input and there is a larger gap before the next pair, the pairs become obvious.
The working rule is simple: the space inside a group must be smaller than the space between groups.
Use a spacing scale
Random margins produce random groupings. As with the type scale, pick a small set of spacing values and reuse them. A common choice is a base unit multiplied up, such as 0.5rem, 1rem, 2rem, and 4rem.
:root {
--space-1: 0.5rem;
--space-2: 1rem;
--space-3: 2rem;
--space-4: 4rem;
}
.card {
padding: var(--space-2);
}
.card h2 {
margin: 0 0 var(--space-1);
}
.card p {
margin: 0;
}
.card + .card {
margin-top: var(--space-3);
}Here the heading and its paragraph are 0.5rem apart. Two cards are 2rem apart. The 4:1 ratio makes each card read as one unit.
Worked example: a heading that floats
A common error is a heading with equal space above and below it. The heading then looks as close to the previous section as to its own content.
/* Weak grouping: equal space above and below */
.article h2 {
margin: 1.5rem 0;
}
/* Stronger grouping: more space above than below */
.article h2 {
margin: 2.5rem 0 0.5rem;
}The second rule pulls the heading toward the text it introduces. It also separates it from the section above. Apply the same idea to figures and captions, and to list titles and their lists.
Other ways to group
Spacing is not the only grouping cue. These work alongside it:
| Technique | How it groups | Typical use |
|---|---|---|
| Whitespace | Distance between items | Form fields, article sections |
| Background panel | A shared fill behind items | Cards, sidebars |
| Border or divider | A line around or between items | Table rows, list items |
| Alignment | A shared edge | Columns of text and labels |
Prefer whitespace first. Borders and panels add visual noise, and each one competes for attention. Add them only when spacing alone is not enough.
Spacing and emphasis
Whitespace also raises priority. An element surrounded by generous space draws the eye, because nothing crowds it. A call-to-action button with extra margin around it can stand out as much as a larger one. Use this carefully. If every element has generous space, none of them stands out.
.hero {
display: grid;
gap: var(--space-2);
padding-block: var(--space-4);
}
.hero .button--primary {
margin-top: var(--space-3);
}Common errors
- Uniform spacing everywhere. Equal gaps erase groups. Vary the gaps on purpose.
- Inside gap larger than outside gap. A card with 2rem of internal spacing and 1rem between cards reads as a loose pile.
- Mixing margin habits. Setting both a bottom margin on one element and a top margin on the next produces gaps you did not plan. Pick one direction, or use
gapin flex and grid layouts. - Spacing in pixels only. Fixed pixel values do not follow the reader's font size. Use
remoremso spacing scales with text.
Summary
Closeness signals relationship. Keep gaps inside groups small, gaps between groups larger, and take both from a shared scale. Add borders and panels only when spacing is not enough.
Exercise
A card has 1.5rem of space between its title and its description, and 1rem of space between the card and the next card. Readers say the cards look jumbled. What is wrong, and how would you fix it using a spacing scale of 0.5rem, 1rem, 2rem, and 4rem?
Show answer
The space inside the card (1.5rem) is larger than the space between cards (1rem), so the title looks closer to the neighboring card than to its own description. Reduce the title-to-description gap to 0.5rem and increase the gap between cards to 2rem. The inside gap is then smaller than the outside gap, and each card reads as one group.
Exercise
Why is margin: 2.5rem 0 0.5rem a better rule for a section heading than margin: 1.5rem 0?
Show answer
The first rule puts more space above the heading than below it. The heading then sits close to the text it introduces and apart from the previous section. The second rule gives equal space on both sides, so the heading looks like it belongs to neither section.