Sub topic 4
Building a spacing scale with custom properties
This chapter assumes you know the CSS box model, can read rem units, and have seen a CSS custom property (a name starting with --) before. Here you will define a small set of spacing values once and use only those values for every gap on the page.
Why a limited set of values helps
Suppose one gap is 14px, another is 16px and a third is 18px. The eye cannot tell these apart, so they don't signal grouping. They also make the stylesheet harder to maintain.
The proximity principle works only when differences in distance are noticeable. A spacing scale forces that. You pick a handful of values that are clearly different from each other, and every margin, padding and gap must come from that set.
Defining the scale
Store the scale as custom properties on :root, so every element can read them.
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 1rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
--space-6: 4rem;
}These values are called design tokens: named values that carry a design decision. Three choices are worth noting.
- Units are
rem. Spacing then grows when the user raises their browser's default font size. Pixel values would stay fixed. - Steps grow by roughly 1.5 to 2 times. Each step is visibly larger than the one before it. A scale of 1rem, 1.05rem, 1.1rem would defeat its purpose.
- Names are numbers, not sizes. With
--space-3you can add a value between two steps later without renaming everything, which--space-mediummakes awkward.
What each step is for
| Token | Value | Typical use |
|---|---|---|
| `--space-1` | 0.25rem | Label to its input, icon to text |
| `--space-2` | 0.5rem | Tight lists, small button padding |
| `--space-3` | 1rem | Paragraphs, field to field |
| `--space-4` | 1.5rem | Card padding, gap between cards |
| `--space-5` | 2.5rem | Group to group |
| `--space-6` | 4rem | Section to section |
Smaller tokens express a closer relationship. The later chapters on cards and forms rely on this mapping.
Using the tokens
Reference a token with var() wherever you would write a length.
.card {
padding: var(--space-4);
border: 1px solid #ccc;
}
.card h2 {
margin: 0 0 var(--space-2);
}The diagram shows the flow: one definition, many consumers.
Changing --space-4 now updates every card and every grid gap at once.
Common errors
- Raw values sneaking in. A stray
margin: 18pxbreaks the scale. Search your stylesheet forpxand unexplainedremvalues during review. - A misspelled token. If
var(--space-9)is not defined, the declaration is invalid at computed-value time and the property falls back to its initial value, usually 0. The layout collapses with no error message. Check DevTools if spacing vanishes. - Too many steps. Ten or more tokens bring the original problem back. Start with six and add one only when a real need appears.
- Using the scale for non-spacing values. Font sizes and border widths belong in their own scales.
Exercise
A stylesheet contains padding: 14px, padding: 16px and padding: 18px on three similar components. Rewrite them using the scale from this chapter, and say which token you chose and why.
Show answer
Use padding: var(--space-3) (1rem) for all three. The three pixel values are too close to read as intentional differences, and 16px equals 1rem, the nearest token. If one component truly needs more room, move it to var(--space-4) so the difference is visible, and use rem-based tokens so the padding scales with the user's font size.