Tailwind CSS Handbook v4.3

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.

ClassValueClassValue
p-00p-61.5rem / 24px
p-0.50.125rem / 2pxp-82rem / 32px
p-10.25rem / 4pxp-102.5rem / 40px
p-20.5rem / 8pxp-123rem / 48px
p-30.75rem / 12pxp-164rem / 64px
p-41rem / 16pxp-205rem / 80px
p-51.25rem / 20pxp-246rem / 96px

v4 generates these dynamically, so any multiple works: p-13, p-72, mt-96 are all valid without configuration.

Rescale the whole system with one variable
@theme {
  --spacing: 0.2rem;
}

Padding#

UtilityApplies to
p-4All sides
px-4 / py-4Horizontal / vertical
pt-4 pr-4 pb-4 pl-4Individual sides
ps-4 / pe-4Logical start / end — flips in RTL
pbs-4 / pbe-4Logical block start / end v4.2
Loading preview…

Margin#

UtilityApplies to
m-4, mx-4, my-4, mt-4…Same pattern as padding
mx-autoCentre a block element with a width
-mt-4Negative margin — pull an element outwards
ms-4 / me-4Logical start / end
mbs-4 / mbe-4Logical block start / end v4.2

Negative margins#

Useful for overlap and for cancelling a parent's padding:

HTML
<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.

UtilityPurpose
gap-4Both axes
gap-x-4 / gap-y-2Per axis
Loading preview…

space-x-* and space-y-*#

These add margin to every child except the last, for cases where you cannot use flex or grid.

HTML
<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?#

SituationUse
Space inside an elementp-*
Space between siblings in a flex/grid containergap-*
Space between siblings with no flex/gridspace-y-*
Space around one specific elementm-*
Centring a fixed-width blockmx-auto
Pulling an element outside its parent-m-*

Common patterns#

Consistent vertical rhythm#

HTML
<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#

HTML
<section class="px-6 py-12 sm:py-16 lg:px-8 lg:py-24">…</section>

Divided list#

Loading preview…

divide-y draws borders between children without a trailing line on the last one.

Form field stack#

HTML
<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#

  • gap without flex or grid. It does nothing on a block element.
  • Fighting margin collapse. Switch to gap or add flow-root to the parent.
  • Mixing space-y-* and gap. Pick one per container.
  • Using arbitrary values for common spacing. p-[18px] breaks the rhythm; p-4 or p-5 almost always reads better.
  • Padding on an inline element expecting vertical space. Horizontal padding applies; vertical padding overlaps neighbouring lines. Use inline-block.