Sub topic 8
The spacing scale and the --spacing base value
This chapter assumes you know how @theme defines tokens and how namespaces map variables to utilities. It covers the one variable that drives Tailwind's whole spacing scale, how to change it, and how to add named spacing values next to it.
One base value instead of a long list
In Tailwind v3 the spacing scale was a fixed table of entries such as 1: 0.25rem, 2: 0.5rem and 3: 0.75rem. In v4 the default theme defines a single variable:
@theme {
--spacing: 0.25rem;
}Utilities that use the spacing scale multiply this value by the number in the class name. The class p-4 produces:
.p-4 {
padding: calc(var(--spacing) * 4);
}With the default base, p-4 is 0.25rem * 4 = 1rem. The number in the class is a multiplier, not a lookup key. That has two consequences:
- Any number works without being declared.
p-13,mt-27andw-91all resolve, because the multiplier is applied to the base value. - Changing the base value rescales every spacing utility at once.
Decimal multipliers work too. p-2.5 becomes calc(var(--spacing) * 2.5), which is 0.625rem by default. Negative utilities flip the sign of the multiplier. -mt-4 becomes margin-top: calc(var(--spacing) * -4).
Which utilities use the scale
Many utilities read from the spacing scale, not only padding and margin. The common ones:
| Utility family | Example | CSS produced (default base) |
|---|---|---|
| Padding | `px-6` | `padding-inline: calc(var(--spacing) * 6)` (1.5rem) |
| Margin | `mt-2` | `margin-top: calc(var(--spacing) * 2)` (0.5rem) |
| Gap | `gap-4` | `gap: calc(var(--spacing) * 4)` (1rem) |
| Width and height | `w-64`, `h-10` | `width: calc(var(--spacing) * 64)` (16rem) |
| Size | `size-8` | width and height of 2rem |
| Inset | `top-3` | `top: calc(var(--spacing) * 3)` (0.75rem) |
| Translate | `translate-x-5` | translation of 1.25rem |
The fractional width utilities such as w-1/2 and keyword utilities such as w-full do not use the base value. They keep their own definitions, so changing --spacing leaves them alone.
Utilities in other namespaces are also unaffected. max-w-md reads from --container-md, and text-lg reads from --text-lg. Changing the spacing base does not resize type or containers.
Changing the base value
To make every spacing utility larger or smaller, override --spacing in your own @theme block:
@import "tailwindcss";
@theme {
--spacing: 0.3rem;
}Now p-4 is 1.2rem, gap-6 is 1.8rem, and w-10 is 3rem. No class in your HTML changes.
You can also set the base in pixels:
@theme {
--spacing: 4px;
}Pick the unit on purpose. A rem base scales with the user's browser font-size setting, which is usually what you want for accessibility. A px base stays fixed. Do not mix the two and expect the layout to respond the same way to zoom and font settings.
Because this changes everything, check the places where spacing and type interact. If --spacing grows by 20% and your font sizes stay put, paddings around text will look looser than before.
Adding named spacing values
The base value covers numeric steps. For named values, use the --spacing-* namespace. Each variable you define creates a name that all the spacing-based utilities accept:
@import "tailwindcss";
@theme {
--spacing-gutter: 1.5rem;
--spacing-section: 6rem;
}<section class="py-section">
<div class="px-gutter grid gap-gutter">
<div class="h-gutter w-gutter bg-neutral-200"></div>
</div>
</section>Here py-section, px-gutter, gap-gutter, h-gutter and w-gutter all exist, because every spacing-based utility reads the --spacing-* namespace. Named values are plain lengths. They are not multiplied by anything, and they do not change when you change --spacing.
That last point matters for design systems. If you define --spacing-gutter: 1.5rem and later set --spacing: 0.3rem, the numeric scale grows but gutter stays at 1.5rem. If you want the named value to track the base, define it in terms of the base:
@theme {
--spacing: 0.3rem;
--spacing-gutter: calc(var(--spacing) * 5);
}Now gutter is 1.5rem today and 1.75rem if you later raise the base to 0.35rem. The calc() expression is evaluated by the browser, so it follows the variable.
Reading the scale in your own CSS
Since --spacing is a regular CSS variable at runtime, custom CSS can use it directly:
.card {
padding: calc(var(--spacing) * 4);
margin-block: calc(var(--spacing) * 6);
}Tailwind v4 also provides a shorthand function that expands to the same calc() expression:
.card {
padding: --spacing(4);
margin-block: --spacing(6);
}Both forms stay in sync with utilities, because they read the same base. If you change --spacing, hand-written CSS and utility classes move together. This is the main reason to prefer these forms over a hard-coded 1rem.
Which variables reach the output
Tailwind only emits theme variables that your project actually uses. If any utility in your HTML reads --spacing, for example p-4, the compiled CSS includes the --spacing variable declaration in the theme layer. If nothing uses it, it is left out.
Custom CSS that reads var(--spacing) directly does not count as a use unless Tailwind detects the reference. To make sure the variable always exists, declare the theme with the static option:
@import "tailwindcss";
@theme static {
--spacing: 0.25rem;
--spacing-gutter: 1.5rem;
}This is covered in more detail in the chapter on @theme static. For spacing, the practical rule is that a .card rule using var(--spacing) is safe when some utility also uses it, and safest with static.
Common mistakes
- Redefining numeric steps. Writing
--spacing-4: 1remdoes not replace the multiplier forp-4. Change the base, or use a named value. - Expecting fractions to scale.
w-1/2is always 50%. Only multiplier-based classes follow--spacing. - Using a unitless base.
--spacing: 4makescalc(var(--spacing) * 4)the number16instead of a length, and the property is ignored. The base needs a unit. - Changing the base without checking components. Third-party components that use utility classes will resize too. Test them.
- Hard-coding lengths beside utilities.
padding: 1remin custom CSS ignores the base. Usevar(--spacing)or--spacing().
Exercise
A project sets --spacing: 0.5rem in @theme and also defines --spacing-gutter: calc(var(--spacing) * 3). What are the computed values of p-4, px-gutter and w-1/2 on an element inside a 400px wide parent?
Show answer
p-4 is calc(0.5rem * 4) = 2rem of padding. px-gutter is calc(0.5rem * 3) = 1.5rem of horizontal padding, because the named value is defined in terms of the base and follows it. w-1/2 is 50% of the parent, which is 200px. The fractional width does not use --spacing.
Exercise
You want every numeric spacing utility to be 20% larger, but you want py-section to stay at exactly 6rem. Write the @theme block.
Show answer
Raise the base from 0.25rem to 0.3rem and define the named value as a fixed length:
@theme {
--spacing: 0.3rem;
--spacing-section: 6rem;
}
Numeric utilities such as p-4 now use 0.3rem as the base. py-section stays at 6rem, because named values are not multiplied by the base.