間距
Use spacing tokens, base-unit multipliers, and precise low-level values for layout rhythm.
Overview
Spacing controls the rhythm inside components, between related elements, and across page sections. Master CSS maps the default spacing scale to spacing variables, so padding, margin, gap, and inset can share the same design language.
| Token | Value | PX | Representation |
|---|---|---|---|
--spacing-4xs | .125rem | 2px | |
--spacing-3xs | .25rem | 4px | |
--spacing-2xs | .375rem | 6px | |
--spacing-xs | .5rem | 8px | |
--spacing-sm | .75rem | 12px | |
--spacing-md | 1rem | 16px | |
--spacing-lg | 1.5rem | 24px | |
--spacing-xl | 2rem | 32px | |
--spacing-2xl | 3rem | 48px | |
--spacing-3xl | 4rem | 64px | |
--spacing-4xl | 6rem | 96px | |
--spacing-5xl | 8rem | 128px |
Use the scale by relationship:
4xsto2xsfor micro spacing between icons, tags, compact controls, and inline text.xstosmfor form fields, list rows, toolbar controls, and tight component groups.mdas the default component rhythm.lgtoxlfor card padding, grouped content, and larger component separation.2xlto5xlfor layout gutters, section spacing, and large editorial rhythm.
<div class="grid-cols:4 gap:md p:md"> <div>1</div> <div>2</div> ...</div>Generated CSS
@layer theme { :root { --spacing-md: 1rem }}@layer utilities { .gap\:md { gap: var(--spacing-md) } .p\:md { padding: var(--spacing-md) } .grid-cols\:4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)) }}Namespace for spacing
The spacing variable namespace is shared across padding, margin, gap, inset, scroll spacing, and similar layout rules.
| Group | Utility keys |
|---|---|
| Margin | m, mt, mr, mb, ml, mx, my, mxs, mxe, mys, mye |
| Padding | p, pt, pr, pb, pl, px, py, pxs, pxe, pys, pye |
| Gap and inset | gap, gap-x, gap-y, inset, top, right, bottom, left, ix, iy, ixs, ixe, iys, iye |
| Scroll spacing | scroll-m, scroll-mt, scroll-mr, scroll-mb, scroll-ml, scroll-mx, scroll-my, scroll-p, scroll-pt, scroll-pr, scroll-pb, scroll-pl, scroll-px, scroll-py |
| Other native spacing | border-spacing, outline-offset, text-indent, text-underline-offset, word-spacing, translate, transform-origin, object-position, background-position |
This lets spacing-related utilities use md, lg, or project token names without the spacing- prefix.
@theme { --spacing-sm: 0.75rem; --spacing-md: 1rem; --spacing-lg: 1.5rem; --spacing-wide: 6rem;}Apply the custom spacing tokens in your markup:
<section class="py:wide"> <div class="mt:md p:sm gap:lg">...</div></section>Using spacing multiplier
The x suffix multiplies the base unit theme setting, which defaults to 4 pixels. Use it for measured geometry that should stay aligned with the spacing grid but does not need a named token.
For example, 1x is 0.25rem, 2x is 0.5rem, 3x is 0.75rem, and 8x is 2rem.
<div class="m:1x"></div> <!-- 4px, margin: 0.25rem --><div class="p:2x"></div> <!-- 8px, padding: 0.5rem --><div class="w:8x"></div> <!-- 32px, width: 2rem --><div class="gap:3x"></div> <!-- 12px, gap: 0.75rem -->For product surfaces, prefer named spacing tokens such as sm, md, and lg. Use x values for measured local geometry, such as icon size, avatar size, skeleton blocks, or a component-specific fixed width.
Exact pixel values
Use 0 for resets, explicit 1px for hairlines, and other px units when a value needs exact pixel geometry. Bare numbers are emitted as written, so p:1 becomes padding: 1 and is not a valid CSS length.
<div class="m:0">0px, margin: 0rem</div><div class="p:1px">1px, padding: 1px</div><div class="w:2px">2px, width: 0.125rem</div>Avoid mixing named spacing tokens, x multipliers, and exact pixel values arbitrarily. Prefer named tokens for layout rhythm, x values for measured local geometry, and px values only for hairlines or exact pixel adjustments.
Common mistakes
Do not use numeric keys as tokens
Avoid defining spacing tokens with numeric keys.
@theme { --spacing-1: 0.25rem; --spacing-2: 0.5rem; }Numeric keys are easy to confuse with low-level CSS values. Prefer semantic token keys like sm or card, then use p:1px for a 1px value or p:1x for the default 4px base unit.
Use descriptive scale or role keys.
@theme { --spacing-sm: 0.75rem; --spacing-compact: 1.25rem; --spacing-relaxed: 6rem;}Summary
Master CSS provides a unified system for defining spacing:
- Use named spacing tokens for shared rhythm.
- Use
xmultipliers for measured local geometry. - Use
0,1px, or explicit pixel units for resets and exact pixel adjustments. - Avoid numeric token keys so markup stays clear.