Sub topic 4
Building a spacing scale with custom properties
Prerequisites and scope
This chapter assumes you know basic CSS selectors, the box model, and rem and em units. It also assumes you have read the earlier chapters on the proximity principle and on padding versus margin. Here you will define a set of spacing values once, store them as CSS custom properties, and use only those values for every gap on a page.
The goal is simple. If every distance on your page comes from a short list, the distances you choose will mean something. A gap of 1rem will always signal the same level of relationship. A gap of 2.5rem will always signal a weaker one.
Why arbitrary spacing breaks grouping
The proximity principle says that elements placed close together look related. That only works if the viewer can compare distances. Suppose one card has 13px of padding, the next has 15px, and a third has 18px. The eye sees small differences as mistakes, not as signals. The page looks slightly off, and you cannot say why.
A spacing scale fixes this by limiting your choices. You pick a handful of sizes, give them names, and use nothing else. Two benefits follow:
- Visible differences are intentional. If two gaps differ, they differ by a clear step, so the viewer reads them as different levels of grouping.
- Decisions get faster. You no longer ask "should this be 14px or 16px?" You ask "is this a tight relationship or a loose one?"
Custom properties as design tokens
A custom property is a CSS variable. You declare it with a name that starts with two dashes, and you read it with var(). When a value like a spacing size is stored this way and reused everywhere, it is often called a design token.
Here is a small scale:
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 1rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
--space-6: 4rem;
}:root matches the <html> element, so every element on the page inherits these properties. Custom properties inherit by default, which is why declaring them once on :root is enough.
The values are in rem, which is relative to the root font size. With the common browser default of 16px, the scale works out as follows:
| Token | Value | Pixels at 16px root | Typical use |
|---|---|---|---|
| `--space-1` | 0.25rem | 4px | Label to input, icon to text |
| `--space-2` | 0.5rem | 8px | Tight lists, related controls |
| `--space-3` | 1rem | 16px | Paragraph and field spacing |
| `--space-4` | 1.5rem | 24px | Card padding, grid gaps |
| `--space-5` | 2.5rem | 40px | Between groups of fields |
| `--space-6` | 4rem | 64px | Between major page sections |
Because the unit is rem, the whole scale grows if a user raises their browser's default font size. That is a good property, and a later chapter returns to it.
Choosing the steps
The scale above is not strictly mathematical. The steps are 4, 8, 16, 24, 40, 64 pixels. Early steps grow by small amounts, and later steps grow by larger ones. There are two common ways to build a scale.
- Linear multiples of a base unit. Use 4px or 8px as the base and multiply: 4, 8, 12, 16, 24, 32, and so on. This is easy to remember and fits pixel grids.
- A ratio. Multiply each step by a fixed number, such as 1.5, to get 4, 6, 9, 13.5, 20. Each step is clearly larger than the last, so levels stay distinct.
Whichever you choose, check one thing: neighboring steps must look different. If --space-3 and --space-4 are only 2px apart, the viewer cannot tell them apart, and you will end up using them interchangeably. That defeats the purpose. A step ratio of roughly 1.5 between neighbors keeps levels distinguishable. The scale above is slightly steeper at the top (24 to 40 is about 1.67, and 40 to 64 is 1.6), which suits section-level gaps that need to stand out.
Six steps is enough for most sites. A larger scale tempts you to use near-duplicate values.
Naming tokens
There are two common naming styles.
- Numbered:
--space-1through--space-6. Short, and it is clear that bigger numbers mean more space. - T-shirt sizes:
--space-xs,--space-s,--space-m,--space-l,--space-xl. Easy to read, but you run out of names when you need to insert a step.
Avoid names that describe where a value is used, such as --card-padding. That is a different layer, covered below.
Using the scale
Apply the tokens through var() wherever you would write a length. This example uses padding for the space inside a card, and gap for the space between cards:
.card {
padding: var(--space-4);
border: 1px solid #ccc;
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-4);
}Padding makes space inside each card, and gap makes equal space between the cards without any margins on the children. Both use --space-4, so the inside and outside of the cards share one rhythm.
Notice what the scale does for grouping. The space inside a card is the same as the space between cards. That can be a deliberate choice, but it can also weaken grouping, because the viewer has less distance to separate "inside" from "between." When you want cards to read clearly as separate units, make the gap between them at least as large as the padding inside them, and often larger. You will refine this in the chapter on cards and sections.
A rule for grouping with the scale
A useful guideline is that the space around a group should be larger than the space inside it. Using the scale, this becomes concrete:
- Space between a label and its input:
--space-1. - Space between two fields in the same group:
--space-3. - Space between two groups:
--space-5.
Each level up skips at least one step. Skipping steps makes the difference between levels obvious. If you went from --space-3 to --space-4 for the next level, the viewer might not notice a difference at all.
Layering tokens: primitives and roles
As a project grows, a flat list of sizes can become hard to reason about. A common solution is two layers.
- Primitive tokens are the raw scale:
--space-1to--space-6. - Role tokens give a purpose to a primitive:
--card-padding,--stack-gap,--section-padding.
:root {
--space-3: 1rem;
--space-4: 1.5rem;
--space-6: 4rem;
--card-padding: var(--space-4);
--stack-gap: var(--space-3);
--section-padding: var(--space-6);
}
.card {
padding: var(--card-padding);
}Role tokens let you change a whole category at once. If you decide cards need more breathing room, you edit one line. You still never invent a value outside the scale, because a role token points to a primitive.
Do not add role tokens early. Start with the primitives. Introduce a role token only when you find yourself repeating the same choice in several places.
Overriding tokens locally
Custom properties follow the cascade, so you can redefine a token for one part of the page. Child elements then pick up the new value.
.compact {
--space-3: 0.75rem;
--space-4: 1rem;
}
.card {
padding: var(--space-4);
}Any .card inside a .compact container now has 1rem of padding, and any .card elsewhere keeps 1.5rem. This is powerful for dense areas such as sidebars or data tables.
Use it sparingly, though. Redefining a primitive changes its meaning in that region. If --space-3 is 1rem in most places and 0.75rem in a sidebar, a reader of the CSS cannot rely on the number alone. A safer pattern is to override a role token or a purpose-built variable, and leave the primitives fixed. The flow-spacing chapter shows this with a --flow-space variable.
Common errors
Mixing units. Writing margin: 17px next to padding: var(--space-3) breaks the system. Search your stylesheet for raw px and rem lengths in spacing properties. Each one is either a deliberate exception or a bug.
Using px in the scale. Pixel tokens do not scale with the user's font-size preference. Use rem for spacing that should follow text size.
A scale that is too fine. If you have steps at 12, 14, 16, and 18 pixels, you have not built a scale. You have built a list of options that look the same.
Using the scale for everything. Borders, outline widths, and icon sizes are not spacing. A 1px border should stay 1px. Putting it in the spacing scale would cause odd results.
Forgetting the fallback. var(--space-3) fails silently if the property is not defined, and the declaration becomes invalid at computed-value time. For a property like padding, that means it falls back to its initial value (0), not to the value you meant. When a token might be missing, provide a fallback as the second argument: var(--flow-space, var(--space-3)).
Typos in token names. A misspelled name such as --space-03 produces the same silent failure. If padding disappears, inspect the element in your browser's developer tools. The Computed panel shows whether the custom property resolved.
Checking the scale in practice
After you define the tokens, run a quick audit.
- Search the stylesheet for
margin,padding, andgapdeclarations that do not usevar(--space-. - For each match, decide: replace it with the nearest token, or document why it is an exception.
- Open the page and look for two gaps that are almost, but not quite, the same size. Those usually mean a stray raw value.
A quick search from a terminal can help:
grep -nE "(margin|padding|gap)[a-z-]*:\s*[0-9.]+(px|rem|em)" styles.cssThis prints every spacing declaration that uses a raw length. Lines that use 0 or auto are not matched, which is usually what you want.
Summary of decisions
| Question | Recommendation |
|---|---|
| Which unit? | `rem`, so spacing follows user text size |
| How many steps? | About six, with neighbors clearly different |
| Where to declare? | `:root`, so every element inherits |
| When to add role tokens? | After you repeat a choice in several places |
| When to override locally? | Rarely, and prefer role tokens over primitives |
| How to group with it? | Skip at least one step between levels |
Exercise
A page has these spacing values: 0.25rem between a label and its input, 1rem between fields, and 1.25rem between two groups of fields. Using the six-step scale from this chapter, what is wrong with the group spacing, and which token would you use instead?
Show answer
The 1.25rem gap between groups is only slightly larger than the 1rem gap between fields, so the viewer cannot see the groups as separate. It is also not on the scale. Use --space-5 (2.5rem) between groups. That skips --space-4, so the difference between field spacing (--space-3) and group spacing is clear.
Exercise
You write padding: var(--space-35); on a card, but no --space-35 token exists. What happens, and how would you find the problem?
Show answer
The browser does not report an error. The declaration is invalid at computed-value time, so padding falls back to its initial value of 0 and the card has no padding. Inspect the card in developer tools and check the Computed panel, where padding shows as 0 and the custom property does not resolve. Fix the name to an existing token such as --space-3.
Exercise
Why is it safer to override a role token like --card-padding in a compact sidebar than to redefine --space-4 there?
Show answer
Redefining --space-4 changes what that primitive means for every element inside the sidebar, including elements that use it for other purposes, and it makes the CSS harder to read because the same token name has different values in different places. Overriding --card-padding changes only the one decision you meant to change, and the primitives stay fixed.