Spacing
Margin, padding, gap and space-between in Tailwind — the single spacing scale that drives them, and which tool to reach for when.
Spacing in Tailwind comes from one scale. Learn it once and it applies to padding, margin, gap, width, height and inset alike.
The scale#
--spacing defaults to 0.25rem, and every numeric spacing utility is a multiple of it.
| Class | Value | Class | Value |
|---|---|---|---|
p-0 | 0 | p-6 | 1.5rem / 24px |
p-0.5 | 0.125rem / 2px | p-8 | 2rem / 32px |
p-1 | 0.25rem / 4px | p-10 | 2.5rem / 40px |
p-2 | 0.5rem / 8px | p-12 | 3rem / 48px |
p-3 | 0.75rem / 12px | p-16 | 4rem / 64px |
p-4 | 1rem / 16px | p-20 | 5rem / 80px |
p-5 | 1.25rem / 20px | p-24 | 6rem / 96px |
v4 generates these dynamically, so any multiple works: p-13, p-72, mt-96 are all valid without configuration.
@theme {
--spacing: 0.2rem;
}Padding#
| Utility | Applies to |
|---|---|
p-4 | All sides |
px-4 / py-4 | Horizontal / vertical |
pt-4 pr-4 pb-4 pl-4 | Individual sides |
ps-4 / pe-4 | Logical start / end — flips in RTL |
pbs-4 / pbe-4 | Logical block start / end v4.2 |
<div class="space-y-2 p-4 text-xs">
<div class="inline-block bg-indigo-100"><div class="bg-white p-2 text-indigo-900">p-2</div></div>
<div class="inline-block bg-indigo-100"><div class="bg-white px-6 py-2 text-indigo-900">px-6 py-2</div></div>
</div>Margin#
| Utility | Applies to |
|---|---|
m-4, mx-4, my-4, mt-4… | Same pattern as padding |
mx-auto | Centre a block element with a width |
-mt-4 | Negative margin — pull an element outwards |
ms-4 / me-4 | Logical start / end |
mbs-4 / mbe-4 | Logical block start / end v4.2 |
Negative margins#
Useful for overlap and for cancelling a parent's padding:
<div class="p-6">
<img class="-mx-6 w-[calc(100%+3rem)]" src="…" alt="">
</div>Margin collapsing#
Vertical margins between siblings collapse to the larger of the two — mb-4 next to mt-8 yields 32px, not 48px. This surprises people constantly. gap does not collapse, which is one more reason to prefer it.
Gap#
gap is the modern default for spacing children. It requires a flex or grid container.
| Utility | Purpose |
|---|---|
gap-4 | Both axes |
gap-x-4 / gap-y-2 | Per axis |
<div class="flex flex-wrap gap-3 p-4 text-sm">
<span class="rounded bg-indigo-600 px-3 py-1.5 text-white">One</span>
<span class="rounded bg-indigo-600 px-3 py-1.5 text-white">Two</span>
<span class="rounded bg-indigo-600 px-3 py-1.5 text-white">Three</span>
<span class="rounded bg-indigo-600 px-3 py-1.5 text-white">Four</span>
</div>space-x-* and space-y-*#
These add margin to every child except the last, for cases where you cannot use flex or grid.
<div class="space-y-4">
<p>First</p>
<p>Second</p>
</div>The same change applies to divide-x-* and divide-y-*.
Which spacing tool?#
| Situation | Use |
|---|---|
| Space inside an element | p-* |
| Space between siblings in a flex/grid container | gap-* |
| Space between siblings with no flex/grid | space-y-* |
| Space around one specific element | m-* |
| Centring a fixed-width block | mx-auto |
| Pulling an element outside its parent | -m-* |
Common patterns#
Consistent vertical rhythm#
<article class="space-y-6">
<h2 class="text-2xl font-semibold">Heading</h2>
<p>Paragraph.</p>
<p>Another paragraph.</p>
</article>Section padding that scales#
<section class="px-6 py-12 sm:py-16 lg:px-8 lg:py-24">…</section>Divided list#
<ul class="mx-auto max-w-sm divide-y divide-gray-200 rounded-lg border border-gray-200 text-sm">
<li class="px-4 py-3 text-gray-700">Inbox</li>
<li class="px-4 py-3 text-gray-700">Drafts</li>
<li class="px-4 py-3 text-gray-700">Sent</li>
</ul>divide-y draws borders between children without a trailing line on the last one.
Form field stack#
<form class="flex flex-col gap-5">
<label class="flex flex-col gap-1.5">…</label>
<label class="flex flex-col gap-1.5">…</label>
</form>Common mistakes#
gapwithoutflexorgrid. It does nothing on a block element.- Fighting margin collapse. Switch to
gapor addflow-rootto the parent. - Mixing
space-y-*andgap. Pick one per container. - Using arbitrary values for common spacing.
p-[18px]breaks the rhythm;p-4orp-5almost always reads better. - Padding on an
inlineelement expecting vertical space. Horizontal padding applies; vertical padding overlaps neighbouring lines. Useinline-block.
Related topics#
- Sizing — widths and heights from the same scale
- Flexbox and Grid — where
gaplives - Theme & Design Tokens — customising
--spacing