Skip to main content

Tailwind CSS Margin & Padding Cheatsheet

Complete Tailwind v4 spacing reference with visual examples.
Click any class to copy to clipboard.

Spacing Scale Reference

This scale applies to all spacing utilities: padding (p-*), margin (m-*), gap-*, and space-*.

SizeValue (rem)Pixels
00px0px
px1px1px
0.50.125rem2px
10.25rem4px
1.50.375rem6px
20.5rem8px
2.50.625rem10px
30.75rem12px
3.50.875rem14px
41rem16px
51.25rem20px
61.5rem24px
71.75rem28px
82rem32px
92.25rem36px
102.5rem40px
112.75rem44px
123rem48px
143.5rem56px
164rem64px
205rem80px
246rem96px
287rem112px
328rem128px
369rem144px
4010rem160px
4411rem176px
4812rem192px
5213rem208px
5614rem224px
6015rem240px
6416rem256px
7218rem288px
8020rem320px
9624rem384px

Padding Utilities

Replace {size} with any value from the spacing scale above (0, px, 0.5, 1, 2, 4, 8, etc.)

ClassCSS
p-{size}padding: {value};
px-{size}padding-left: {value}; padding-right: {value};
py-{size}padding-top: {value}; padding-bottom: {value};
pt-{size}padding-top: {value};
pr-{size}padding-right: {value};
pb-{size}padding-bottom: {value};
pl-{size}padding-left: {value};
ps-{size}padding-inline-start: {value};
pe-{size}padding-inline-end: {value};

Padding - Visual Examples

p-4 (all sides)

Content

px-6 (horizontal)

Content

py-6 (vertical)

Content

pt-8 (top only)

Content

pr-8 (right only)

Content

pb-8 (bottom only)

Content

Margin Utilities

Includes auto values for centering and positioning

ClassCSS
m-{size}margin: {value};
mx-{size}margin-left: {value}; margin-right: {value};
my-{size}margin-top: {value}; margin-bottom: {value};
mt-{size}margin-top: {value};
mr-{size}margin-right: {value};
mb-{size}margin-bottom: {value};
ml-{size}margin-left: {value};
ms-{size}margin-inline-start: {value};
me-{size}margin-inline-end: {value};
m-automargin: auto;
mx-automargin-left: auto; margin-right: auto;
my-automargin-top: auto; margin-bottom: auto;
mt-automargin-top: auto;
mr-automargin-right: auto;
mb-automargin-bottom: auto;
ml-automargin-left: auto;

Margin - Visual Examples

m-4 (all sides)

Content

mx-auto (center horizontally)

Centered

mt-8 (top margin)

Content

ml-auto (push right)

Pushed Right

Negative Margin Utilities

Prefix with - to use negative values

ClassCSS
-m-{size}margin: -{value};
-mx-{size}margin-left: -{value}; margin-right: -{value};
-my-{size}margin-top: -{value}; margin-bottom: -{value};
-mt-{size}margin-top: -{value};
-mr-{size}margin-right: -{value};
-mb-{size}margin-bottom: -{value};
-ml-{size}margin-left: -{value};

Negative Margin - Visual Example

Negative margins pull elements outside their container or overlap with siblings.

-mt-6 -ml-6
Item 1
Item 2 (-ml-8)
Item 3

Space Between Utilities

Add space between child elements using the adjacent sibling combinator

ClassCSS
space-x-{size}& > * + * { margin-left: {value}; }
space-y-{size}& > * + * { margin-top: {value}; }
space-x-reverse& > * + * { --tw-space-x-reverse: 1; }
space-y-reverse& > * + * { --tw-space-y-reverse: 1; }

Space Between - Visual Examples

Adds margin between child elements without affecting the first or last child.

space-x-4 (horizontal)

1
2
3

space-y-4 (vertical)

1
2
3

space-x-8 (larger gap)

1
2
3

space-x-reverse (RTL)

1
2
3

Gap Utilities

Works with flexbox and grid layouts. Preferred over space-* for most use cases.

ClassCSS
gap-{size}gap: {value};
gap-x-{size}column-gap: {value};
gap-y-{size}row-gap: {value};

Gap - Visual Examples

Gap utilities work with flex and grid layouts. Preferred over space-* for most cases.

Flexbox with gap-4

1
2
3
4

Grid with gap-4

1
2
3
4
5
6

Grid with gap-x-8 gap-y-2 (different axes)

1
2
3
4
5
6

Logical Properties (ps-*, pe-*, ms-*, me-*)

Logical properties adapt to text direction (LTR/RTL). Use these for internationalization.

ps-8 (padding-inline-start) - LTR

Content (LTR)

ps-8 (padding-inline-start) - RTL

Content (RTL)

ms-auto (margin-inline-start: auto) - LTR

Pushed End

ms-auto (margin-inline-start: auto) - RTL

Pushed End

Common Patterns

Centered Container

<div class="mx-auto max-w-4xl px-4">
  <!-- Content centered with padding -->
</div>

Card with Consistent Padding

<div class="p-6 lg:p-8">
  <!-- Responsive padding -->
</div>

Vertical Stack

<div class="flex flex-col gap-4">
  <Card />
  <Card />
  <Card />
</div>

Section Spacing

<section class="py-16 lg:py-24">
  <!-- Responsive vertical spacing -->
</section>

Button with Padding

<button class="px-4 py-2">
  Click me
</button>

Push Footer to Bottom

<main class="flex-1">
  <!-- Content -->
</main>
<footer class="mt-auto">
  <!-- Footer sticks to bottom -->
</footer>

About Tailwind Spacing

Tailwind uses a consistent spacing scale based on 0.25rem (4px) increments. This creates visual harmony across your design.

Gap vs Space: Prefer gap-* for flexbox and grid layouts as it doesn't affect the first/last child and works in both directions.

For responsive layouts, prefix any class with a breakpoint: md:p-8, lg:gap-6