Sub topic 2
The spacing scale: one number for every length
This chapter explains the spacing scale, the single set of numbers that Tailwind uses for padding, margin, gap, width, height, and more. It assumes you know basic HTML and the class attribute, the CSS box model, CSS units (px, rem, percentages), and that you have a project with Tailwind CSS installed.
What the scale is
Tailwind does not ask you to write lengths such as 1rem or 16px in most utilities. Instead, you write a number, and Tailwind turns it into a length. That set of numbers is the spacing scale.
The rule for the default scale is simple: one unit equals 0.25rem. With the browser default of 16px per rem, one unit is 4px.
/* The idea behind the scale */
/* p-1 -> padding: 0.25rem (4px) */
/* p-4 -> padding: 1rem (16px) */
/* p-8 -> padding: 2rem (32px) */So p-4 means "four units of padding", which is 1rem. You can do the arithmetic in your head: multiply the number by 4 to get pixels at the default root font size.
A few common values:
| Class suffix | Length in rem | Length in px (at 16px root) |
|---|---|---|
| 0 | 0 | 0 |
| 0.5 | 0.125rem | 2px |
| 1 | 0.25rem | 4px |
| 2 | 0.5rem | 8px |
| 4 | 1rem | 16px |
| 6 | 1.5rem | 24px |
| 8 | 2rem | 32px |
| 12 | 3rem | 48px |
| 16 | 4rem | 64px |
There is also a px step. It is a literal one pixel, not a multiple of the unit. So p-px gives padding: 1px, and border-spacing or w-px use it the same way.
Where the same numbers appear
The scale is shared. The number after the dash means the same length in every utility that uses the scale. This is why the idea is "one number for every length".
<div class="p-4 mt-4 w-64 h-16 gap-4">
...
</div>In this class list, the 4 in p-4, mt-4, and gap-4 is 1rem. The 64 in w-64 is 16rem, and the 16 in h-16 is 4rem.
The scale is used by:
- Padding (
p-*,px-*,py-*,pt-*, and so on) - Margin (
m-*,mx-*,mt-*, and so on), including negative margins - Gap and space between children (
gap-*,space-x-*,space-y-*) - Width and height (
w-*,h-*,size-*) - Positioning offsets such as
inset-*,top-*, andleft-* - Others such as
scroll-m-*andtranslate-*
The practical benefit is consistency. If a card uses p-6 and the text inside it uses mt-6, both use 1.5rem. Your spacing values stay on a short list instead of drifting to 13px in one place and 15px in another.
Why rem and not px
The scale is defined in rem, a unit relative to the root font size of the page. If a user raises their browser's default font size, every rem length grows with it. Padding, margins, and widths then grow in step with the text, so layouts keep their proportions.
If you hard-code pixels with arbitrary values (covered in a later chapter), you give up that behavior for those lengths. For most spacing, the scale is the better default.
How the numbers are laid out
The scale is not a continuous run of every integer. It has fine steps for small values and larger jumps as numbers grow. In Tailwind v3, the default scale looks like this:
- Half steps near zero:
0.5,1.5,2.5,3.5 - Every integer from
1to12 - Then
14,16,20,24,28,32,36,40,44,48,52,56,60,64,72,80,96
This means p-13 does not exist in v3, while p-12 and p-14 do. If you need a value between steps, you have three options: pick the closest step, use an arbitrary value, or add a value to your theme.
Tailwind v4 changes how this works. It computes spacing from a single base variable, --spacing, which defaults to 0.25rem. A class like p-13 generates calc(var(--spacing) * 13), so many more integer values work without being listed in advance. If you are on v4, p-13 is valid. If you are on v3, it is not.
/* Tailwind v4: the base unit is a CSS variable */
@import "tailwindcss";
@theme {
--spacing: 0.25rem;
}Check which version your project uses before relying on unusual numbers. Running the build and seeing no styles for a class is the usual symptom of using a value your version does not define.
Reading a class name as a length
Every spacing utility has the same shape: a property prefix, a dash, and a scale value.
<div class="pt-2 pr-4 pb-6 pl-8">Four sides, four values</div>This maps to:
pt-2:padding-top: 0.5rempr-4:padding-right: 1rempb-6:padding-bottom: 1.5rempl-8:padding-left: 2rem
The prefix chooses the property and side. The number chooses the length. You can swap the prefix without relearning anything: mt-2 is the same 0.5rem as pt-2, applied to margin instead.
The next chapters cover the prefixes in detail. Here, focus on the fact that the number is portable.
Fractions and special keywords
Width and height also accept values that are not on the numeric scale. These are not part of the spacing scale, but they share the same utility names, so it helps to separate them.
| Utility | CSS | Source |
|---|---|---|
| `w-4` | `width: 1rem` | Spacing scale |
| `w-1/2` | `width: 50%` | Fraction |
| `w-full` | `width: 100%` | Keyword |
| `w-screen` | `width: 100vw` | Keyword |
| `w-auto` | `width: auto` | Keyword |
Only the plain numbers (and px) read from the spacing scale. If you customize the scale, w-4 changes but w-1/2 does not.
A worked example
This card uses several scale values. Reading it as numbers makes the design easy to adjust.
<div class="w-80 p-6">
<h2 class="mb-2">Title</h2>
<p class="mb-4">Description text.</p>
<button class="px-4 py-2">Open</button>
</div>The values resolve like this:
w-80is20rem(320px) wide.p-6is1.5rem(24px) of padding on all sides.mb-2is an0.5remmargin under the heading.mb-4is a1remmargin under the paragraph.px-4 py-2is1remof horizontal and0.5remof vertical padding on the button.
Notice the pattern: bigger gaps for bigger separations. The title sits close to its description (2), while the description sits farther from the button (4). Choosing neighboring steps such as 2, 4, 6, 8 gives a visible difference between levels. Choosing 4 and 5 produces differences that are too small to see.
Common mistakes
- Treating the number as pixels.
p-4is16px, not4px. Multiply by 4. - Using a number that is not on the scale in v3.
mt-13orw-15generate nothing. The page shows no error, only missing styles. - Assuming the scale is the same for everything. Font sizes use their own scale (
text-sm,text-lg), covered in the typography chapters.text-4is not a font size of1rem. - Mixing many near-identical values. If a design has
p-5,p-6, andp-7side by side, the differences are4pxapart and rarely intentional. Pick one. - Forgetting the root font size. If your project sets
html { font-size: 62.5% }, one unit becomes2.5px, not4px, and every spacing value shrinks.
Checking the scale in your project
You do not need to memorize the table. To see what a class produces, inspect the element in your browser's developer tools and read the computed CSS. For example, an element with p-6 shows padding: 1.5rem. In v3 you can also open the default theme in tailwindcss/stubs or read the generated CSS to see every available step.
The diagram below shows how a class name resolves to a final length.
Summary of the model
- The spacing scale is a set of numbers where one unit is
0.25remby default. - The same number means the same length in padding, margin, gap, width, height, and offsets.
- Lengths are in
rem, so they follow the user's font size setting. - v3 lists specific steps. v4 computes any integer from
--spacing. - Fractions and keywords such as
w-1/2orw-fullare separate from the scale.
Exercise
A project uses Tailwind v3 with the default theme and the default browser font size. What padding does px-6 produce, in rem and in pixels? Then say whether mt-13 produces any CSS, and what you could do to get a margin of about 52px.
Show answer
px-6 sets left and right padding to 6 x 0.25rem = 1.5rem, which is 24px. In v3, mt-13 is not on the default scale, so no CSS is generated and the element gets no margin. For about 52px (3.25rem), you could use mt-14 (3.5rem, 56px) or mt-12 (3rem, 48px) as the nearest steps, use an arbitrary value such as mt-[52px], or add a spacing key such as 13: '3.25rem' under theme.extend so that mt-13 works.
Exercise
In a card, the title has mb-2 and the paragraph below it has mb-4. A teammate changes the root font size of the page from 16px to 20px using html { font-size: 20px }. What are the new margins in pixels, and why did they change although the classes did not?
Show answer
mb-2 becomes 0.5rem x 20px = 10px and mb-4 becomes 1rem x 20px = 20px. The scale is defined in rem, so every length is relative to the root font size. Changing the root size rescales all spacing utilities together.