Spacing & Sizing
Spacing is the foundation of good design. Tailwind's spacing scale makes it consistent and easy.
The Spacing Scale
Tailwind uses a consistent scale based on 0.25rem (4px):
| Class | Value | Pixels |
|---|---|---|
| p-0 | 0 | 0px |
| p-1 | 0.25rem | 4px |
| p-2 | 0.5rem | 8px |
| p-3 | 0.75rem | 12px |
| p-4 | 1rem | 16px |
| p-6 | 1.5rem | 24px |
| p-8 | 2rem | 32px |
| p-12 | 3rem | 48px |
| p-16 | 4rem | 64px |
Padding
Control inner spacing with padding utilities:
<!-- All sides -->
<div class="p-4">Padding all around</div>
<!-- Horizontal (left & right) -->
<div class="px-4">Padding left and right</div>
<!-- Vertical (top & bottom) -->
<div class="py-4">Padding top and bottom</div>
<!-- Individual sides -->
<div class="pt-4">Padding top only</div>
<div class="pr-4">Padding right only</div>
<div class="pb-4">Padding bottom only</div>
<div class="pl-4">Padding left only</div>
See it live:
Margin
Control outer spacing with margin utilities:
<!-- All sides -->
<div class="m-4">Margin all around</div>
<!-- Auto margins for centering -->
<div class="mx-auto w-64">Centered horizontally</div>
<!-- Negative margins (useful for overlaps) -->
<div class="-mt-4">Pull up by 1rem</div>
Centered card example:
Centered Card
This card is centered with mx-auto and max-w-sm
Width
Control element width:
<!-- Fixed widths -->
<div class="w-32">128px wide</div>
<div class="w-64">256px wide</div>
<!-- Percentage widths -->
<div class="w-1/2">50% wide</div>
<div class="w-full">100% wide</div>
<!-- Max widths -->
<div class="max-w-md">Max 28rem</div>
Width comparison:
Height
Control element height:
<!-- Fixed heights -->
<div class="h-16">64px tall</div>
<div class="h-32">128px tall</div>
<!-- Full height -->
<div class="h-full">100% of parent</div>
<div class="h-screen">100vh (full viewport)</div>
Space Between
For spacing children without margins:
<!-- Vertical spacing between children -->
<div class="space-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Horizontal spacing between children -->
<div class="flex space-x-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
See it live:
Using space-x-4 creates 1rem gap between items
Practical Example: Dashboard Stats
Let's combine spacing utilities:
<div class="max-w-4xl mx-auto p-6">
<h1 class="text-2xl font-bold mb-6">Dashboard</h1>
<div class="grid grid-cols-3 gap-4">
<div class="bg-white p-4 rounded-lg shadow">
<div class="text-3xl font-bold">2,847</div>
<div class="text-gray-500 text-sm mt-1">Total Users</div>
</div>
...
</div>
</div>
See it live:
Key Takeaways
- Spacing scale: Multiply by 0.25rem (p-4 = 1rem = 16px)
- Directional: x = horizontal, y = vertical, t/r/b/l = individual sides
- Centering: Use
mx-autowith a set width - Percentages: Use fractions like
w-1/2,w-2/3 - Space between:
space-x-andspace-y-for child spacing
Next, let's explore colors and typography!