agyanadda@gmail.com

Tailwind Spacing

Tailwind Layout 1 min read Updated Dec 1, 2026 Topic 9 of 27

Padding and margin

p-* = padding, m-* = margin. Add a side letter:

Side Padding Margin
all p-4 m-4
left + right px-4 mx-4
top + bottom py-4 my-4
top pt-4 mt-4
right pr-4 mr-4
bottom pb-4 mb-4
left pl-4 ml-4

The number × 0.25rem: 4 = 1rem, 8 = 2rem, px = 1px.

Useful extras

  • mx-auto — centre a block with a width
  • Negative margin: -mt-4
  • space-y-4 — vertical space between children
  • space-x-2 — horizontal space between children
  • gap-4 — space inside flex and grid layouts (preferred)

Responsive spacing

p-4 md:p-8 lg:p-12

Example

Example — Padding, margin and space HTML
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

<div class="p-6">
  <div class="bg-sky-100 p-2 mb-2">p-2</div>
  <div class="bg-sky-100 p-6 mb-2">p-6</div>
  <div class="bg-sky-100 px-10 py-2 mb-6">px-10 py-2</div>
  <div class="bg-indigo-600 text-white w-60 mx-auto p-4 text-center rounded">mx-auto centres me</div>
  <div class="space-y-3 mt-6">
    <div class="bg-amber-100 p-3 rounded">space-y-3</div>
    <div class="bg-amber-100 p-3 rounded">adds gaps</div>
    <div class="bg-amber-100 p-3 rounded">between children</div>
  </div>
</div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

How much padding is p-8?

Exercise 2 Fill in the Blank

space-___-4 adds vertical space between child elements.

Exercise 3 True / False

-mt-4 is a negative top margin.

Frequently Asked Questions

Use gap inside flex and grid containers. Use space-y for simple stacks of normal block elements.