Sub topic 8
Spacing and Proximity: Grouping Related Content
Earlier chapters covered size, contrast, and position. This chapter covers the fourth tool, empty space. Readers decide which items belong together mostly from how far apart the items are, before they read anything. This chapter assumes you can write CSS with custom properties, know the box model, and know the difference between margin and padding. It also assumes you have used Flexbox or Grid at a basic level.
Proximity: distance as a statement about relationships
The proximity principle says that items placed close together are seen as a group, and items placed far apart are seen as separate. This happens before reading. A reader glancing at a page sees clusters first and text second.
This makes spacing a hierarchy tool. A cluster behaves like one element, so it competes for attention as a single unit. Spacing also controls how many units a page seems to contain. A page with 40 evenly spaced items looks like 40 equal things. The same page with 6 clusters looks like 6 things, and the reader can pick one.
Proximity is relative. A 24px gap looks large between two lines of a caption and small between two page sections. Readers judge each gap by comparing it with the gaps around it.
The core rule: space within a group is smaller than space between groups
If you remember one rule, remember this one: the gap inside a group must be clearly smaller than the gap between groups. Use a difference large enough to see. A gap of 16px inside a group and 20px between groups looks like a mistake, not a decision. A ratio of about 1.5 to 2 or more is a good starting point, and larger ratios work for major page sections.
The rule also applies to elements that look unrelated. A label that sits 2rem above its input and 1rem below the previous input will read as belonging to the previous input, even though the markup says otherwise.
| Relationship | Example | Suggested space |
|---|---|---|
| Tightly bound | Label and its input; heading and its first paragraph | 0.25rem to 0.5rem |
| Same group | Paragraphs in an article; items in a list | 1rem |
| Related groups | Fields in a form; cards in a row | 1.5rem |
| Separate sections | Hero and feature list; article and comments | 2.5rem to 4rem |
These values are starting points. What matters is that each step is visibly larger than the one before it.
Build a spacing scale
For type, the previous chapters used a scale so that sizes differ predictably. Spacing benefits from the same approach. If you pick a new margin for each element, gaps will differ by small, accidental amounts, and those differences read as noise.
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 1rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
--space-6: 4rem;
}Each step is about 1.5 to 1.7 times the previous one, which keeps neighboring steps distinguishable. Use rem so spacing grows when users increase their browser's default font size. Do not use a scale with steps like 16, 18, 20, 22 pixels. Those steps are too close to tell apart on screen, so they add choices without adding meaning.
Tools for spacing and their pitfalls
CSS gives you several ways to create space. Each has different behavior, and the differences cause most spacing bugs.
paddingis space inside an element's border. It moves content away from the element's own edge, and it takes the element's background color.marginis space outside the border. It is transparent and pushes neighbors away.gapis space between children in Flexbox and Grid. It applies only between items, never at the outer edges.
Vertical margins in normal block layout collapse. When the bottom margin of one block meets the top margin of the next, the browser uses the larger of the two, not the sum. A margin-bottom: 1rem on a paragraph followed by margin-top: 2rem on a heading gives a 2rem gap. Margins do not collapse inside Flex or Grid containers, so the same CSS can produce different gaps after a layout change.
To avoid this, choose one direction for margins and stick to it. A common pattern is to put space only on the top of an element, based on what comes before it. This is sometimes called the lobotomized owl selector (* + *).
.prose > * + * {
margin-top: var(--space-3);
}This gives every direct child except the first a 1rem top margin. It never leaves extra space at the start or end of the container, and no element needs to know what follows it.
Headings belong to what follows them
A heading introduces the content below it. The space above a heading should therefore be larger than the space below it. With equal spacing above and below, a heading floats between two paragraphs and the reader cannot tell which one it labels.
You can add exceptions to the owl rule above for headings:
.prose > * + * {
margin-top: var(--space-3);
}
.prose > * + :is(h2, h3) {
margin-top: var(--space-6);
}
.prose > :is(h2, h3) + * {
margin-top: var(--space-2);
}The second rule opens a large gap before any heading. The third rule shrinks the gap after a heading, which binds the heading to its first block. The third rule comes last, so a heading directly followed by another heading gets the small gap. Both rules have the same specificity, so source order decides. If you reorder them, the result changes, which is a common source of confusion.
Forms: where proximity errors are most costly
Forms show proximity errors clearly, because a mismatched label and field causes wrong input, not just a poor impression. Keep each label and its input as one tight unit, and separate units with a larger gap.
.form {
display: grid;
gap: var(--space-4);
}
.field {
display: grid;
gap: var(--space-1);
}
.field label {
font-weight: 600;
}
.field input {
padding: var(--space-2) var(--space-3);
}Here the label-to-input gap is 0.25rem and the field-to-field gap is 1.5rem, a ratio of 6. Nobody will attach a label to the wrong input. Note that you set the two levels on two different containers: the .form container controls the space between fields, and each .field controls the space inside one field. That separation is the general method: every group gets its own container, and each container owns the spacing of its direct children.
Padding inside groups
A group that has a background, border, or shadow, like a card, needs padding. The padding should be at least as large as the largest gap inside the card. If the internal gap is 1.5rem and the padding is 0.5rem, the content looks cramped against the card's edge, and the edge no longer reads as a boundary.
.card {
display: grid;
gap: var(--space-2);
padding: var(--space-4);
background: #f4f6fb;
border-radius: 8px;
}
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: var(--space-4);
}Look at the pairing: padding inside each card is --space-4, and the gap between cards is also --space-4. If the gap between cards matched the gap inside them (--space-2), cards would blur together. If you make the outer gap larger than the padding, cards separate clearly. A good check is that the space around a card's content is similar to or smaller than the space between cards.
Proximity versus other grouping cues
Space is not the only way to group. A background color, a border, or a divider line also creates a group, and designers call this the common region cue. Proximity is the lightest option because it adds no visual elements. Use it first.
Use a stronger cue when proximity alone is not enough:
- Items within a group have uneven heights, so gaps appear uneven.
- The layout is dense, such as a data table or a settings page, and you cannot spare enough whitespace.
- A group must read as an interactive container, such as a clickable card.
Avoid stacking all cues. A border, a background color, and wide spacing around the same group add noise. Each extra cue also competes with the size and contrast cues from earlier chapters.
Decision flow for a gap
Common errors
- Equal spacing everywhere. Every element has
margin: 1rem. The page has no clusters, so nothing is grouped and the reader must read everything. - Space that contradicts the markup. A caption sits closer to the next image than to its own image. Readers trust the space, not the HTML.
- Mixing margin directions. Some elements use
margin-bottomand others usemargin-top. Collapsing makes the results hard to predict, and a layout change can silently alter the gaps. - Pixel gaps that ignore text zoom. Gaps in
pxstay fixed while text grows, so grouping gets weaker as users enlarge text. Useremorem. - Tiny differences. A 12px gap next to a 14px gap looks like a rendering glitch.
- Excess whitespace. Very large gaps between every element split the page into islands. A reader has to scroll to compare related items. Keep related items on the same screen where possible.
Edge cases
Line height is spacing too. Line height controls space between lines of one paragraph. Keep it smaller than the paragraph gap, or paragraphs stop reading as units. With line-height: 1.5 and a paragraph gap of 1rem (about two thirds of a line), paragraphs are only barely separated. A gap of 1em to 1.5em is more reliable.
WCAG text spacing. WCAG 2.1 success criterion 1.4.12 requires that content still works when users override line height to 1.5, paragraph spacing to 2 times the font size, letter spacing to 0.12em, and word spacing to 0.16em. Fixed-height containers cut off text under these overrides. Build groups from padding and gap and avoid fixed heights, so groups grow with their content.
Spacing on small screens. On narrow screens, large section gaps use too much of the viewport. Reduce the top steps of the scale, but keep their order. The ratio between within-group and between-group gaps matters more than the absolute sizes. You will see how to do this in the responsive chapter.
Dynamic content. User-generated text may be short or long. A layout that relies on equal heights to look grouped will break. Use gaps, not fixed heights, to separate items.
Exercise
A form has labels that sit 1.5rem above their inputs, and a 1rem gap between one input and the next label. Users often type an answer into the wrong field. Explain the cause using proximity, and give the CSS change that fixes it.
Show answer
Each input is closer to the next label (1rem) than to its own label (1.5rem), so readers group it with the wrong label. Make the label-to-input gap the smallest in the form and the field-to-field gap larger. For example, use .field { display: grid; gap: var(--space-1); } for each label and input pair, and .form { display: grid; gap: var(--space-4); } between fields.
Exercise
A page uses margin-bottom: 1.5rem on paragraphs and margin-top: 1rem on h2 elements. The designer expected 2.5rem above each heading, but the gap is 1.5rem. Why, and what is a reliable fix?
Show answer
Adjacent vertical margins in block layout collapse to the larger value, not the sum, so the gap is max(1.5rem, 1rem) = 1.5rem. A reliable fix is to put space in one direction only. Use .prose > * + * { margin-top: var(--space-3); } and .prose > * + h2 { margin-top: var(--space-6); }, with no bottom margins on the children.