Sub topic 2
The spacing scale: one number for every length
This chapter assumes you know the CSS box model, rem and px units, and how to put utility classes in a class attribute. It also assumes a working Tailwind project. The previous chapter showed that each utility class maps to a CSS declaration. Here you will see how the number in a class such as p-4 becomes a length.
One scale, many properties
Tailwind does not use a separate set of values for each property. Padding, margin, gap, width, height, and several others read from the same spacing scale. If you learn that 4 means 1rem, you also know what p-4, mt-4, gap-4, and w-4 do.
How a number becomes a length
One step on the scale is 0.25rem. With the browser default of 16px per rem, that is 4px. To find a length, multiply the number by 0.25rem.
| Class | CSS value | Pixels at 16px root |
|---|---|---|
| `p-px` | `1px` | 1px |
| `p-0.5` | `0.125rem` | 2px |
| `p-1` | `0.25rem` | 4px |
| `p-2` | `0.5rem` | 8px |
| `p-4` | `1rem` | 16px |
| `p-6` | `1.5rem` | 24px |
| `p-8` | `2rem` | 32px |
| `p-12` | `3rem` | 48px |
The values are in rem, so they follow the root font size. If a user raises their browser's default text size, the spacing grows with it. The one exception is px, which is fixed at one pixel. It is useful for hairline borders and gaps.
The same number in different places
Reuse of the scale keeps a layout consistent. In the next snippet, every length comes from the same set of steps, so the card's padding, its gap, and its icon size line up.
<div class="flex items-center gap-4 p-4">
<div class="h-12 w-12 bg-gray-200"></div>
<p class="mt-2">Padding, gap, and icon size share one scale.</p>
</div>The generated CSS for the classes above is roughly:
.gap-4 { gap: 1rem; }
.p-4 { padding: 1rem; }
.h-12 { height: 3rem; }
.w-12 { width: 3rem; }
.mt-2 { margin-top: 0.5rem; }What numbers are allowed
This depends on your Tailwind version.
- Tailwind v3 ships a fixed list of steps. It runs
0,px,0.5,1,1.5,2,2.5,3,3.5, then every integer up to12. After that it moves in larger jumps:14,16,20,24, and so on up to96. A class such asp-13does not exist, and Tailwind generates no CSS for it. - Tailwind v4 computes each value from a single base variable,
--spacing, which defaults to0.25rem. A class such asp-13produces3.25rem, so you can use any number without extending the theme.
Named steps such as p-gutter can be added to either version through the theme. A later chapter covers that.
Common mistakes
- Reading the number as pixels.
p-4is 16px, not 4px. - Guessing a step that does not exist in v3, such as
m-13. The class is silently ignored. Check the generated CSS or the docs when a spacing class has no effect. - Using
p-pxwhen you meantp-1. The first is 1px, the second is 4px.
Exercise
A design asks for 40px of padding on a card and 12px between its children. Which padding and gap classes do you use, assuming the default 16px root font size?
Show answer
Use p-10 for the padding (40 / 4 = 10, which is 2.5rem) and gap-3 for the gap (12 / 4 = 3, which is 0.75rem).
Exercise
In Tailwind v3, what happens if you write mt-13, and why?
Show answer
Nothing is applied. The default v3 scale has no 13 step, so Tailwind generates no CSS for the class. Use mt-12 or mt-14, or add a custom value to the theme. In v4 the class would work and give 3.25rem.