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 already builds CSS from your files. Spacing and sizing utilities control the space around and inside elements and how big those elements are.
The spacing scale
Tailwind uses one numeric scale for padding, margin, gap, width, and height. In the default theme, each step is a multiple of a base unit of 0.25rem, which is 4px at the default font size. So 4 means 1rem (16px), and 6 means 1.5rem (24px).
| Class | CSS value | Pixels (at 16px root) |
|---|---|---|
| `p-1` | `0.25rem` | 4px |
| `p-2` | `0.5rem` | 8px |
| `p-4` | `1rem` | 16px |
| `p-6` | `1.5rem` | 24px |
| `p-8` | `2rem` | 32px |
Because every utility draws from the same scale, a p-4 card and a gap-4 grid line up visually without extra effort.
Padding and margin
The class name is a property letter plus an optional side, then a scale value. p is padding and m is margin.
| Prefix | Applies to |
|---|---|
| `p-`, `m-` | all four sides |
| `px-`, `mx-` | left and right |
| `py-`, `my-` | top and bottom |
| `pt-`, `pr-`, `pb-`, `pl-` | one side (top, right, bottom, left) |
Negative margins put a minus sign in front of the class, as in -mt-2. Use mx-auto to center a block element that has a set width.
<div class="mx-auto mt-8 max-w-sm p-6">
<h2 class="mb-2 text-xl font-semibold">Title</h2>
<p class="px-2 py-1">Text with its own padding.</p>
</div>Space between children
Two utilities space a group of children without putting a margin on each one.
gap-*works on flex and grid containers.gap-x-*andgap-y-*set one axis.space-x-*andspace-y-*add margin between siblings of any block or flex container.
Prefer gap when the parent is already flex or grid. It does not depend on sibling selectors and it handles wrapped rows correctly.
<ul class="flex flex-wrap gap-4">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>Width and height
Width and height use the same scale, plus fractions and keywords.
| Class | Result |
|---|---|
| `w-64` | `16rem` wide |
| `w-1/2` | 50% of the parent |
| `w-full` | 100% of the parent |
| `w-screen` | 100% of the viewport width |
| `h-screen` | 100% of the viewport height |
| `w-fit` | as wide as its content |
The size-* utility sets width and height together, so size-10 is shorter than w-10 h-10. It is available in Tailwind v3.4 and later.
To limit growth or shrinkage, use min-w-*, max-w-*, min-h-*, and max-h-*. A common pattern is a fluid element with an upper limit: w-full max-w-md.
<div class="flex min-h-screen items-center justify-center">
<img class="size-24" src="/avatar.png" alt="Profile photo">
<div class="w-full max-w-md">A fluid column capped at 28rem.</div>
</div>Common errors
- Vertical margins on inline elements. Elements such as
<span>ignore top and bottom margin. Addinline-blockorblock. - Percentage heights that do nothing.
h-fullonly works if the parent has a defined height. - Mixing
space-*andgap-*. Pick one per container, or the spacing will add up in surprising ways. - Using
w-screenfor full-width layouts. It includes the scrollbar width and can cause horizontal scrolling. Usew-fullinstead. - Combining
w-fullwith padding. Tailwind setsbox-sizing: border-boxin its base styles, so padding stays inside the width. If you disable the base styles, that guarantee goes away.
How the pieces relate
Exercises
Exercise
You want a container that is full width on small screens but never wider than 32rem, centered horizontally, with 1.5rem of padding on all sides. Which classes do this?
Show answer
w-full max-w-lg mx-auto p-6. max-w-lg is 32rem, mx-auto centers it, and p-6 is 1.5rem.
Exercise
A flex row of three buttons needs 8px between them. Which utility is the best choice, and why?
Show answer
gap-2 on the flex container. 2 on the scale is 0.5rem (8px), and gap works with wrapped rows without relying on sibling selectors.