Sub topic 3
Spacing and sizing utilities
This chapter assumes you know the CSS box model (content, padding, border, margin), can read basic HTML, and have a Tailwind project that builds CSS, as covered in the previous chapter. Here you will learn how Tailwind names spacing and size utilities, how the underlying scale works, and the mistakes that most often surprise people.
The spacing scale
Tailwind does not ask you to invent pixel values. Padding, margin, gap, width, height, and many other utilities share one numeric scale. In Tailwind v4, the scale comes from a single base value, --spacing, which defaults to 0.25rem. A utility such as p-4 computes its value as the number multiplied by that base.
| Class number | Calculation | Value at 16px root font size |
|---|---|---|
| 1 | 1 × 0.25rem | 4px |
| 2 | 2 × 0.25rem | 8px |
| 4 | 4 × 0.25rem | 16px |
| 6 | 6 × 0.25rem | 24px |
| 8 | 8 × 0.25rem | 32px |
| 16 | 16 × 0.25rem | 64px |
Because the values are in rem, they grow if the user raises the browser's default font size. This keeps layouts accessible, but it also means the pixel values above hold only at the usual 16px root size.
Padding and margin
Padding is space inside an element, between its content and its border. Margin is space outside the border, between the element and its neighbors. Tailwind uses p for padding and m for margin, followed by an optional side and then a number.
| Suffix | Meaning | Example |
|---|---|---|
| (none) | All four sides | `p-4` |
| `x` | Left and right | `px-4` |
| `y` | Top and bottom | `py-2` |
| `t`, `r`, `b`, `l` | One physical side | `pt-6`, `ml-2` |
| `s`, `e` | Start and end of the inline direction | `ps-4`, `me-2` |
The s and e utilities follow the text direction. In a left-to-right language, ps-4 pads the left side. In a right-to-left language such as Arabic, it pads the right side. Use them when your interface must support both directions.
<div class="rounded border p-4">
<h2 class="mb-2 text-lg font-semibold">Order summary</h2>
<p class="px-2 py-1">Two items in your cart.</p>
<p class="mt-4 ms-auto w-fit">Total: $42</p>
</div>Negative and auto margins
A leading dash makes a margin negative. -mt-2 pulls an element up by 0.5rem, which is useful for overlaps such as an avatar that straddles a banner edge. Padding cannot be negative in CSS, so there is no -p-2.
mx-auto sets both horizontal margins to auto. On a block element with a fixed or maximum width, this centers it. It does nothing on an element that is already full width, which is a common source of confusion.
<main class="mx-auto max-w-3xl px-4">
Centered content column with side padding on small screens.
</main>Gap and space between children
When you place several children in a flex or grid container, gap is usually the best way to separate them. gap-4 sets the space between rows and columns. gap-x-4 and gap-y-2 set each axis independently.
Tailwind also has space-x-* and space-y-*. These add a margin to every child except the first. They work in simple stacks, but they have known edge cases. They do not behave well when children wrap onto a new line, when children are hidden, or when the order is reversed. gap has none of these problems, so prefer it whenever the parent is a flex or grid container.
<ul class="flex flex-col gap-2">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>Width and height
Sizing utilities use w for width and h for height. They accept several kinds of values.
| Kind | Example | CSS result |
|---|---|---|
| Scale number | `w-64` | `width: 16rem` |
| Fraction | `w-1/2` | `width: 50%` |
| Full | `w-full` | `width: 100%` |
| Viewport | `h-screen` | `height: 100vh` |
| Content based | `w-fit`, `w-min`, `w-max` | `fit-content`, `min-content`, `max-content` |
| Automatic | `w-auto` | `width: auto` |
When width and height are equal, use size-*. For example, size-10 is the same as w-10 h-10. It keeps avatar and icon markup short.
<img class="size-12 rounded-full" src="/avatar.png" alt="Profile photo">Minimum and maximum sizes
Fixed widths break on small screens, so limits are often safer. max-w-* sets an upper bound, and min-w-* and min-h-* set lower bounds. Named container sizes such as max-w-sm, max-w-md, and max-w-3xl are common for readable columns.
A pattern worth memorizing is w-full max-w-md. The element fills its parent on a narrow screen and stops growing at a comfortable width on a wide one.
<form class="w-full max-w-md">
<input class="w-full rounded border p-2" type="email" placeholder="Email">
</form>For full-height sections, prefer min-h-screen over h-screen. A fixed height clips content when the content is taller than the screen. A minimum height lets the section grow.
How these pieces affect layout
Two ideas explain most spacing surprises. First, the box model decides what width means. Tailwind's base styles (called Preflight) set box-sizing: border-box on every element. With that setting, w-64 p-4 produces a box that is 16rem wide in total, and the padding is subtracted from the content area. Without it, the padding would be added on top and the box would be wider.
Second, margins on block elements can collapse. Adjacent vertical margins, such as mb-4 on one paragraph and mt-6 on the next, merge into one 1.5rem space instead of adding to 2.5rem. This does not happen inside flex or grid containers, which is another reason gap is more predictable.
The diagram below shows the layers of a single element and which utility controls each one.
Custom values and the scale
You are not limited to the default scale. Any number works in v4, so w-72, p-13, and mt-7 are all valid because each is multiplied by the base value. For a value that is not a multiple of the base, use square brackets, as in w-[320px] or p-[13px]. Chapter 13 covers this syntax in more detail.
If your design uses a different rhythm, change the base in your theme. This rescales every spacing, width, and height utility at once.
@import "tailwindcss";
@theme {
--spacing: 0.3rem;
}With this change, p-4 becomes 1.2rem instead of 1rem, and so does every other utility built on the scale.
Common errors
- Fixed widths on small screens.
w-[600px]overflows a 375px phone and causes horizontal scrolling. Usew-full max-w-[600px]instead. - Using
mx-autowithout a width. A block element is already as wide as its parent, so there is nothing to center. Addmax-w-*orw-*. - Using
space-*with wrapping rows. The margins do not reset at line breaks. Switch toflex-wrap gap-*. - Using
h-screenfor sections with variable content. Content that is longer than the viewport is clipped or overflows. Usemin-h-screen. - Expecting percentage heights to work.
h-1/2needs a parent with a defined height. If the parent's height depends on its content, the percentage has nothing to resolve against. - Mixing margin and gap. Adding
m-2to children inside agap-4container adds both, so the visible space is larger than expected.
Worked example
This profile card combines most of the utilities from this chapter. Read each class and name the CSS it produces before you continue.
<article class="mx-auto my-8 w-full max-w-sm p-6">
<div class="flex items-center gap-4">
<img class="size-16 rounded-full" src="/ada.png" alt="Ada Lovelace">
<div>
<h2 class="text-lg font-semibold">Ada Lovelace</h2>
<p class="mt-1 text-sm text-slate-600">Mathematician</p>
</div>
</div>
<p class="mt-4">Wrote the first published algorithm for a machine.</p>
</article>The article is centered by mx-auto, capped at 24rem by max-w-sm, and spaced from neighbors by my-8. The flex row uses gap-4 to separate the image and the text, and size-16 gives the image a 4rem square.
Exercise
A section uses <div class="h-screen p-8"> and its text is cut off on a short phone in landscape orientation. Which change fixes the problem, and why?
Show answer
Replace h-screen with min-h-screen. A fixed height of 100vh clips content that is taller than the viewport, while a minimum height keeps the section at least as tall as the screen and lets it grow with its content.
Exercise
You have a flex row of tags that wraps onto several lines. Using space-x-2 leaves uneven gaps at the line breaks. What do you use instead, and what classes does the container need?
Show answer
Use gap. The container needs flex flex-wrap gap-2. The gap is applied between items on both axes, so wrapped lines keep consistent spacing.
Exercise
With the default v4 spacing base of 0.25rem and a 16px root font size, how wide is an element with w-20, and what single class gives a 48px square?
Show answer
w-20 is 20 × 0.25rem = 5rem, which is 80px. A 48px square is 3rem, so size-12 (12 × 0.25rem) gives it.