How to Build a Spacing Scale That Survives Real UI

Most spacing scales fail at the edges — a 1px border, an icon that needs optical alignment, a card that must fit a fixed height. This guide covers the decisions that hold up in production.

Design tokens Updated

Start from the gap you repeat most

Before choosing a base unit, count the spacing values already in the design. In most interfaces a small set repeats: 4px between an icon and its label, 8px inside a button, 12px between rows, 16px between cards, 24px or 32px between sections. If those numbers are close to multiples of four, a 4px base fits the design instead of fighting it.

The base unit is a filter, not a law. Pick the one that turns most existing values into multiples, then round the rest during migration. Choosing 8px when half the design lives on 12px and 20px creates more exceptions than it removes.

The grid is a filter, not a rule

Grid Strength Cost
4px Half steps for icons, labels and dense UI More values to choose between
8px Fewer decisions, obvious outliers Small components feel cramped

An 8px grid makes a 12px gap look like a mistake, which is useful when several people edit the same file. A 4px grid accepts 12px as a deliberate half step. Neither is more correct; the question is whether your UI has enough small components to need the half step.

Keep three exceptions outside the scale:

  • Hairlines. Borders and dividers stay at 1px, even on a 4px grid. Rounding them to 4px produces a visibly heavy line.
  • Optical alignment. Icons often need 1–2px of adjustment to look centered next to text. Fix it inside the component, not by adding a 2px token.
  • Content-driven sizes. Text measure, image aspect ratios and chart heights follow content, not the spacing scale.

Linear steps for components, geometric for layout

A linear scale adds the same amount at each step: 4, 8, 12, 16, 20, 24. The absolute jump is constant, so the relative jump shrinks as values grow. Going from 4 to 8 doubles the gap; going from 36 to 40 changes it by 11%. That is fine inside a component, where the eye compares neighboring values, and wrong between sections, where small relative changes disappear.

A geometric scale multiplies: 4, 8, 16, 32, 64. The relative jump stays constant, which is why type scales use it. For spacing, a pure geometric scale grows too fast past 32px and leaves a hole between 8 and 16.

Most production systems mix the two: linear up to 12px or 16px, then wider steps above it. A scale like 0, 4, 8, 12, 16, 24, 32, 48, 64 behaves linearly where components need precision and geometrically where layout needs contrast.

Name by step, not by purpose

--space-3 survives a redesign. --space-card-padding does not: the moment the card padding changes, either the token lies or a new one appears. Purpose names also multiply — one per component, then one per breakpoint — until the scale is a list of one-off values with semantic labels.

Use numeric names for the scale and purpose names only for decisions that belong to the brand: page gutter, section rhythm, the gap between a heading and its body. Those are few and change rarely.

Use rem, with px for hairlines

Spacing in rem follows the reader’s default font size. Someone who sets 20px instead of 16px gets gaps that grow with their text. That is the accessible default, and it costs nothing when the root size is 16px: 0.25rem is 4px.

Two places stay in px:

  • Hairlines and borders, so they remain one physical pixel.
  • Values that must match a bitmap, such as a 1px inset shadow or a fixed-height control tied to an icon.

The tool shows both columns because px is easier to reason about while designing and rem is what ships.

Audit before you add

A scale only helps if existing values move onto it. Before adding tokens, list the spacing values in the current UI and count how often each appears. Values used once are usually accidents; values used twenty times are the real scale, even if they are not on a grid.

Then migrate in three passes:

  1. Replace values that are already on the scale. This is mechanical and safe.
  2. Round values within 1px of a scale step. Check screenshots — a 13px gap rounded to 12px is invisible; 20px rounded to 16px is not.
  3. Leave the rest as explicit exceptions, and name them in the component. A documented 18px is better than a token invented to hide it.

When to break the scale

Every scale meets a case it cannot express: a control that must match a 36px icon, a table cell with a fixed line height, a hero that has to fit an exact viewport. Break the scale deliberately, in the component, and write down why. The value of the scale comes from the exceptions being rare and visible, not from pretending they do not exist.

If you want to try the numbers from this guide, the spacing scale generator builds the scale and copies it as CSS variables or a Tailwind theme.

Questions and answers

Do I need a spacing scale for a small project?

A scale pays off as soon as two people touch the UI or the page count passes about ten. Below that, a handful of named values is enough; the cost is in the migration, not the scale itself.

Should spacing tokens be named by size or by purpose?

Name by size. Purpose names such as --space-card-padding couple the token to one component and stop being reusable the moment the layout changes. Reserve purpose names for brand-level decisions that rarely move.

How many steps should a spacing scale have?

Eight to twelve steps covers most interfaces. Fewer forces compromises at small sizes; more creates near-duplicate values that nobody can tell apart.