Sub topic 8
The spacing scale and the --spacing base value
This chapter assumes you know @theme, theme namespaces, and how a theme variable becomes a utility. It covers how one variable, --spacing, drives the whole spacing scale.
One base value, many utilities
Tailwind v4 does not store a fixed list of spacing steps. The default theme defines a single variable:
@theme {
--spacing: 0.25rem;
}A spacing utility multiplies this value by the number in its name. p-4 generates roughly this CSS:
.p-4 {
padding: calc(var(--spacing) * 4);
}With the default of 0.25rem, p-4 is 1rem (16px at the default root font size). p-8 is 2rem, and so on.
The same variable feeds many utilities: padding, margin, gap, width, height, size-*, inset-*, translate-*, scroll margin, and more.
Any number works
Because the utility computes a multiple, you are not limited to a preset list. These classes all work without any configuration:
<div class="p-4 mt-13 w-17 gap-2.5">
<p class="-mb-3">Multiples of the base value</p>
</div>mt-13 is calc(var(--spacing) * 13), which is 3.25rem. Negative utilities such as -mb-3 multiply by -3.
Changing the whole scale
Change --spacing and every numeric spacing utility changes with it:
@import "tailwindcss";
@theme {
--spacing: 0.3rem;
}Now p-4 is 1.2rem, w-10 is 3rem, and gap-2 is 0.6rem. Use this for a denser or looser interface without editing any markup.
Adding named spacing values
The --spacing-* namespace creates named utilities next to the numeric ones:
@import "tailwindcss";
@theme {
--spacing-gutter: 1.5rem;
--spacing-section: 6rem;
}<section class="py-section px-gutter">
<h2 class="mb-4">Named spacing</h2>
</section>py-section, px-gutter, w-gutter and gap-section all exist now. The numeric classes still work, because they come from --spacing.
| Source | Example variable | Example utility |
|---|---|---|
| Base value | `--spacing: 0.25rem` | `p-4`, `mt-13` |
| Named value | `--spacing-gutter: 1.5rem` | `px-gutter` |
| Arbitrary value | none | `p-[13px]` |
Using the scale in your own CSS
--spacing is an ordinary CSS variable, so custom CSS can use it with calc():
.card {
padding: calc(var(--spacing) * 6);
margin-block: calc(var(--spacing) * 3);
}If you later change --spacing, this card scales together with the utilities.
Common mistakes
- Setting
--spacingoutside@theme. A plain:rootvariable does not make Tailwind regenerate its utilities, so thecalc()output may not match your intent. - Expecting
max-w-4to follow--spacing. Max-width scales use the--container-*namespace. - Defining a unitless value such as
--spacing: 4. The result ofcalc(var(--spacing) * 4)has no unit, so the length is invalid.
Exercise
You set --spacing: 0.5rem in @theme. What is the computed value of p-6, and how would you add a m-page utility that equals 5rem?
Show answer
p-6 is calc(0.5rem * 6), which is 3rem. Add --spacing-page: 5rem; inside @theme. Tailwind then generates m-page, along with p-page, w-page, and other spacing utilities.