TExcelTAD
Home › Tailwind CSS › Tailwind Spacing

Tailwind Spacing

Spacing utilities control padding (space inside an element), margin (space outside it) and the space between children. They all use the same numeric scale: 1 = 0.25rem = 4px.

Padding

Class Sides
p-4 all four sides
px-4 / py-4 left + right / top + bottom
pt-4 pr-4 pb-4 pl-4 top / right / bottom / left
ps-4 / pe-4 inline start / end (flip for right-to-left languages)
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-start gap-3 p-4 text-sm">
  <div class="bg-sky-200"><div class="bg-sky-500 text-white">p-0</div></div>
  <div class="bg-sky-200 p-2"><div class="bg-sky-500 text-white">p-2</div></div>
  <div class="bg-sky-200 p-6"><div class="bg-sky-500 text-white">p-6</div></div>
  <div class="bg-sky-200 px-8 py-1"><div class="bg-sky-500 text-white">px-8 py-1</div></div>
  <div class="bg-sky-200 pt-10 pl-2"><div class="bg-sky-500 text-white">pt-10 pl-2</div></div>
</body>
</html>

The light area is the padding; the dark box is the content.

Margin

Margins use the same pattern with m: m-4, mx-4, my-4, mt-4, mr-4, mb-4, ml-4, ms-4, me-4. Two extras:

Class Use
mx-auto centre a block that has a width (e.g. max-w-md mx-auto)
ml-auto push an item to the right inside a flex row
-mt-4 negative margin — pull an element upward
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-gray-100 p-4">
  <div class="mx-auto max-w-xs rounded-lg bg-white p-4 text-center shadow">mx-auto centres me</div>
  <div class="mt-6 flex rounded-lg bg-white p-2 shadow">
    <span class="rounded bg-violet-100 px-2">Logo</span>
    <span class="ml-auto rounded bg-violet-500 px-2 text-white">ml-auto pushes me right</span>
  </div>
  <div class="mx-auto mt-8 h-12 w-40 rounded-lg bg-amber-300"></div>
  <div class="relative mx-auto -mt-6 w-32 rounded-lg bg-amber-600 p-2 text-center text-white">-mt-6 overlap</div>
</body>
</html>

Space between children

Often you want equal gaps between siblings but not before the first or after the last. Two options:

Class How it works Where
gap-4 / gap-x-4 / gap-y-4 CSS gap flex and grid containers (preferred)
space-y-4 / space-x-4 adds margin to every child except the first any parent, e.g. a plain stack of blocks
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="grid grid-cols-2 gap-6 p-4 text-sm">
  <div>
    <p class="mb-2 font-semibold">space-y-3</p>
    <div class="space-y-3">
      <div class="rounded bg-emerald-200 p-2">One</div>
      <div class="rounded bg-emerald-200 p-2">Two</div>
      <div class="rounded bg-emerald-200 p-2">Three</div>
    </div>
  </div>
  <div>
    <p class="mb-2 font-semibold">flex + gap-2</p>
    <div class="flex flex-wrap gap-2">
      <span class="rounded-full bg-pink-200 px-3 py-1">html</span>
      <span class="rounded-full bg-pink-200 px-3 py-1">css</span>
      <span class="rounded-full bg-pink-200 px-3 py-1">tailwind</span>
      <span class="rounded-full bg-pink-200 px-3 py-1">vite</span>
    </div>
  </div>
</body>
</html>

Note: In v4 the space-* utilities use a selector that targets :not(:last-child) and add margin to the bottom/end of each child. They still don't work well with wrapping rows — prefer flex + gap there.

The full scale

Class suffix rem px
0 0 0
px — 1px
0.5 0.125 2
1 0.25 4
2 0.5 8
3 0.75 12
4 1 16
6 1.5 24
8 2 32
12 3 48
16 4 64
any n n × 0.25 n × 4

p-px gives exactly one pixel of padding, useful for hairline details.

Tip: Stick to a few steps (2, 4, 6, 8, 12, 16) across a design. Consistent spacing is one of the easiest ways to make a page look professional.

Test yourself

Which class adds 1rem of padding on the left and right only?

  1. p-4
  2. px-4
  3. py-4
  4. pl-4

How do you horizontally centre a block that has max-w-md?

  1. text-center
  2. mx-auto
  3. m-center
  4. align-center

Which is the preferred way to space items in a wrapping flex row?

  1. space-x-4
  2. gap-4
  3. m-4 on every item
  4. p-4 on the container