In CSS you write padding: 24px or margin-top: 16px, and the number can be anything. In Tailwind, gaps and sizes are written as classes too. The difference is that the numbers come from one scale, like a ruler with a mark every 4px.
There are two groups of classes. Spacing is for gaps: padding (room inside an element, between its content and its edge) and margin (space outside an element, towards its neighbours). Sizing is for size: width and height. Think of parcel boxes on a warehouse shelf. The bubble wrap inside a box is the padding. The gap between two boxes on the shelf is the margin. The size of the box is its width and height.
One step = 4px
The number at the end of a spacing class name is a number of steps. One step = 0.25rem = 4px (1rem = 16px, the browser's default font size).
| Class | Steps | rem | px |
|---|---|---|---|
p-1 |
1 | 0.25rem |
4px |
p-2 |
2 | 0.5rem |
8px |
p-3 |
3 | 0.75rem |
12px |
p-4 |
4 | 1rem |
16px |
p-6 |
6 | 1.5rem |
24px |
p-8 |
8 | 2rem |
32px |
Tailwind v4 calculates the value directly: p-4 becomes padding: calc(var(--spacing) * 4), and --spacing holds 0.25rem. Because the value is calculated, not taken from a fixed list, any multiple of 0.25 works. That includes whole numbers like p-13 (52px) and half steps like p-1.5 (6px). For p-1.3, Tailwind makes no CSS. p-0 means zero padding.
Padding and margin
The * below stands for a number: p-* can be p-4, m-* can be m-2.
p-*= padding: room inside. The element's background colour fills this room too.m-*= margin: space outside, towards the neighbouring element. Margin is always transparent.
Both work as in plain CSS, but you write them as classes.
The direction is the second letter
| Letter | Means | Example | The CSS it makes |
|---|---|---|---|
| (none) | every side | p-4 |
padding |
x |
left and right | px-6 |
padding-inline |
y |
top and bottom | py-3 |
padding-block |
t |
top | mt-4 |
margin-top |
r |
right | pr-2 |
padding-right |
b |
bottom | mb-8 |
margin-bottom |
l |
left | ml-1 |
margin-left |
padding-inline is the padding at the start and end of a line of text. On a page read from left to right, that means left and right. padding-block is the padding at the top and bottom. Here is the box from the exercise:
<div class="box px-6 py-3 mt-4">
Box with spacing
</div>
boxis your own class. Tailwind has noboxclass, so this class adds no styling. Butboxcan still sit next to Tailwind's classes.px-6: left and right padding of 6 × 4px = 24px.py-3: top and bottom padding of 3 × 4px = 12px.mt-4: a top margin of 4 × 4px = 16px, the gap to the element above.
So the left padding is 24px and the top padding is 12px.
Width and height
| Class | The CSS it makes | Result |
|---|---|---|
w-32 |
width: calc(var(--spacing) * 32) |
128px |
h-10 |
height: calc(var(--spacing) * 10) |
40px |
size-10 |
width and height at once |
40px × 40px |
w-1/2 |
width: calc(1/2 * 100%) |
half its parent's width |
w-full |
width: 100% |
as wide as its parent |
w-screen |
width: 100vw |
as wide as the screen |
h-screen |
height: 100vh |
as tall as the screen |
max-w-md |
max-width: var(--container-md) |
at most 28rem (448px) wide |
w-*andh-*use the same 4px scale as padding.mx-auto(left and right margin set toauto) splits the space left over on the left and right equally. Somax-w-md mx-autoputs a block at most 448px wide in the middle.- Tailwind gives every element
box-sizing: border-box. This rule is part of preflight, the base CSS that@import "tailwindcss"also loads. Sow-32is 128px including its padding and border. The browser's default is different: it adds the padding outside the width.
⚠️ Why is the spacing off?
- Reading
pxas pixels. In Tailwind,pxmeans padding-x, which is left and right padding.px-24is 24 × 4px = 96px, not 24px. For 24px, writepx-6. - Mixing up
pyandpt.pt-3only sets the top padding. This exercise asks for 12px of padding on the top and the bottom, so usepy-3. - Deleting the
boxclass. The exercise checks the.boxelement. Add Tailwind's classes next to it:class="box px-6 py-3 mt-4". - Two classes for the same property. For example
p-4 p-8on one element. Which one applies depends on the order of the rules in the CSS Tailwind makes, not the order of the classes in theclassattribute. Use just one.
TL;DR: Spacing in Tailwind is written {p or m}{direction}-{number}. p is padding (room inside an element), m is margin (space outside an element). Direction: no letter means every side, x left-right, y top-bottom, and t r b l one side (top, right, bottom, left). The number is a number of steps, and one step = 0.25rem = 4px. So px-6 = 24px, py-3 = 12px, mt-4 = 16px. Width and height use w-* and h-* (* is a number) on the same scale. There are also w-full (100%), w-1/2 (50%), w-screen (as wide as the screen), max-w-md (at most 448px wide), and mx-auto for centring a block. In Tailwind every element uses border-box, so padding counts inside the width. Remember, px in Tailwind means left-right padding, not pixels.
⚡ The direction is the second letter
Write:
<div class="box px-6 py-3 mt-4">
Box with spacing
</div>
Meaning: p is padding (room inside), m is margin (space outside). The second letter gives the direction. The number at the end is a number of steps, and one step = 4px. So px-6 is 24px of left-right padding, py-3 12px of top-bottom padding, and mt-4 a 16px top margin.
In the exercise, keep the box class. It is your own class, not a Tailwind class. Add Tailwind's classes next to it.
⚡ px isn't pixels
Guess first: is px-24 24px of padding?
No. px means left-right padding, so px-24 is 24 × 4px = 96px. For 24px, write px-6.
Meaning: x is left-right, y top-bottom, and t r b l one side only (top, right, bottom, left).
⚡ Width and height
Meaning: w-* (width) and h-* (height), where * is a number, use the same 4px scale: w-32 = 128px. w-full is as wide as its parent. max-w-md mx-auto puts a block at most 448px wide in the middle: max-w-md limits its width, and mx-auto splits the space left over on the left and right equally.