Spacing¶
Spacing makes a page readable. Use a small scale instead of choosing a new value for every gap.
A spacing scale¶
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 0.75rem;
--space-4: 1rem;
--space-6: 1.5rem;
--space-8: 2rem;
}
Then use only those values:
.card {
padding: var(--space-4);
}
.stack > * + * {
margin-top: var(--space-4);
}
.navbar {
gap: var(--space-4);
}
A scale keeps spacing consistent and makes it easy to adjust the whole page later.
Padding, margin, and gap¶
- Padding is space inside a box. The background covers it.
- Margin is space outside a box. It separates the box from its neighbours.
- Gap adds space between children in flex or grid layout.
Prefer gap over margins for flex and grid children. It adds space only between items, not at the edges.
Logical spacing¶
Logical properties follow the writing direction:
Use them when the page may be translated or displayed in a different writing mode.
A simple stack¶
Or with flexbox:
Both create consistent vertical rhythm. The flex version avoids margin collapse.
Try it¶
- Define a spacing scale with four values.
- Use it for card padding, a stack of paragraphs, and a flex row.
- Change the middle value and see which parts of the page move.
- Replace one margin with
gapin a flex container. - Make the window narrow and check that the spacing still looks reasonable.
If the gaps look uneven, check whether some elements still have default margins.
Next: Pseudo-elements