Spacing System
Three-Layer spacing — size primitives, DNA base, and semantic scale tokens (--fp-s-1 through --fp-s-12).
The Spacing System generates a 12-step spacing scale from one base value (default: 4px). Each semantic step is emitted as a precomputed length (base × the step’s multiplier) times --fp-density, so changing the base in config regenerates every step, and changing density on the root element at runtime rescales them all.
How it works
Spacing follows the Three-Layer API:
Layer 1 — Primitives (--fp-dim-*)
Raw size values as precomputed rem. These are the building blocks:
--fp-dim-2: 0.125rem; /* 2px */
--fp-dim-4: 0.25rem; /* 4px */
--fp-dim-8: 0.5rem; /* 8px */
--fp-dim-16: 1rem; /* 16px */
--fp-dim-32: 2rem; /* 32px */
--fp-dim-64: 4rem; /* 64px */
--fp-dim-128: 8rem; /* 128px */The default theme.css keeps 4, 8, 12, 20, 24, 40, 80 and 128. The rest (2, 6, 10, 16, 32, 48, 64, 96 in the default hybrid mode) are in the full stylesheet only (theme-full.css, or output.tokens: "full").
The primitive scale depends on spacing.mode; a fixed set of values that components use (4, 8, 10, 12, 16, 20, 24, 32, 48, 80, 96, 128) is always included. A custom spacing.scale replaces the scale entirely, so include any of those values your components need.
Layer 2 — DNA (--fp-spacing-*)
The configured base unit (full stylesheet only):
/* 4px — the configured base, folded into every step at build time */
--fp-spacing-base: var(--fp-dim-4);Layer 3 — Semantics (--fp-s-*) — use these
The steps components and layouts read. The table below is generated from this site’s theme config when the site builds.
Base unit: 4px · Mode: hybrid
| Step | Multiplier | px | rem | CSS variable |
|---|---|---|---|---|
| 1 | 0.5× | 2 | 0.125 | --fp-s-1 |
| 2 | 1× | 4 | 0.25 | --fp-s-2 |
| 3 | 1.5× | 6 | 0.375 | --fp-s-3 |
| 4 | 2× | 8 | 0.5 | --fp-s-4 |
| 5 | 3× | 12 | 0.75 | --fp-s-5 |
| 6 | 4× | 16 | 1 | --fp-s-6 |
| 7 | 6× | 24 | 1.5 | --fp-s-7 |
| 8 | 8× | 32 | 2 | --fp-s-8 |
| 9 | 12× | 48 | 3 | --fp-s-9 |
| 10 | 16× | 64 | 4 | --fp-s-10 |
| 11 | 24× | 96 | 6 | --fp-s-11 |
| 12 | 32× | 128 | 8 | --fp-s-12 |
Each step is calc(<base × multiplier, in rem> * var(--fp-density, 1)), wrapped in round(nearest, …, 0.0625rem) so it lands on whole pixels.
How to use
Card with consistent spacing
.card {
padding: var(--fp-s-6); /* 16px */
gap: var(--fp-s-4); /* 8px between children */
border-radius: var(--fp-corner-surface);
}Button padding
.button {
padding: var(--fp-s-2) var(--fp-s-6); /* 4px top/bottom, 16px left/right */
gap: var(--fp-s-2); /* between icon and text */
}Page layout
.page {
padding: var(--fp-s-8); /* 32px page padding */
}
.section {
margin-bottom: var(--fp-s-9); /* 48px between sections */
}
.section-header {
margin-bottom: var(--fp-s-4); /* 8px below heading */
}Flex/grid gaps
.toolbar {
display: flex;
gap: var(--fp-s-3); /* 6px between toolbar items */
}
.grid {
display: grid;
gap: var(--fp-s-6); /* 16px grid gap */
padding: var(--fp-s-8); /* 32px container padding */
}Spacing roles (--fp-gap-*)
The scale says “how much”; the gap roles say “how much for what”. Each recurring relationship has one token that points at a step on the scale, so it follows base changes and the root density:
| Token | Step | Between |
|---|---|---|
--fp-gap-inline | --fp-s-2 | An icon and its label inside one control |
--fp-gap-actions | --fp-s-3 | Sibling controls in an action row (footers, toolbars) |
--fp-gap-items | --fp-s-2 | Repeated items in a collection (list rows, options) |
--fp-gap-header | --fp-s-3 | A title and what sits beside it in a header |
--fp-gap-sections | --fp-s-4 | Blocks inside a surface (header, body, footer) |
--fp-gap-divider | --fp-s-5 | A visible rule and the block it separates |
Override a role to retune that relationship everywhere at once; a component’s own hook still wins.
Density
Density is a single axis — compact (0.75×), default (1×), relaxed (1.25×) — switched with the data-density attribute on <html>. It sets --fp-density, the multiplier every --fp-s-* step scales by, so you don’t touch --fp-spacing-base for this:
<!-- 0.75× spacing on the whole page -->
<html data-density="compact">
...
</html>With data-density on <html>, every --fp-s-* step and --fp-gap-* role recalculates. The steps are declared on :root, so data-density on an inner element (a <section>, a card) does not change them. (Density is how tightly things are packed; how big each thing is belongs to the size axis.)
Inner ≤ outer pattern
.card {
padding: var(--fp-s-6); /* 16px outer */
}
.card-header {
padding: var(--fp-s-3) var(--fp-s-4); /* 6px/8px inner — always smaller */
}
.card-body {
gap: var(--fp-s-3); /* gaps ≤ parent padding */
}Configuration
{
"spacing": {
"base": 4,
"mode": "hybrid"
}
}base: the base unit in px. Default4.mode: how the steps grow —"linear","geometric", or"hybrid"(default). Hybrid is linear for the small steps and geometric for the large ones. At base 4, hybrid gives 2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128px; linear gives 2, 4, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48px; geometric gives 1, 2, 4, 8, 12, 16, 24, 32, 48, 64, 96, 128px.scale(optional): a custom list of primitive values in px. It replaces the generated--fp-dim-*scale; it does not change the--fp-s-*steps.
Best practices
-
Use
--fp-s-*tokens, not raw values.padding: var(--fp-s-4)follows the root density;padding: 8pxdoesn’t. -
Inner spacing ≤ outer spacing. Padding inside a card should be smaller than the space around it. This gives a clear visual hierarchy without extra thought.
-
Use
--fp-s-1to--fp-s-4for component internals (buttons, inputs, badges) and--fp-s-6to--fp-s-9for layout spacing. -
Switch density with
data-densityon<html>; change the base in config. For compact or relaxed spacing usedata-density(it drives--fp-density). To derive all 12 steps from a different base, setspacing.baseintheme-config.jsonand rebuild — the base is folded into each step at build time, so overriding--fp-spacing-baseat runtime does not move the steps. Either way, don’t edit individual--fp-s-*tokens. -
Prefer
gapovermarginbetween siblings.gapworks with flexbox and grid and doesn’t collapse or interact with other margins.