Tailwind CSS/styling
Spacing & Sizing
Tailwind uses one spacing scale for padding, margin, gap, width, height and more. Learning that scale and a handful of sizing helpers lets you build consistent layouts quickly.
4 min readtailwind#tailwind#spacing#sizing
Introduction
Tailwind uses one spacing scale for padding, margin, gap, width, height and more. Learning that scale and a handful of sizing helpers lets you build consistent layouts quickly.
Subtopics
- The spacing scale (
1= 0.25rem) - Padding:
p-*,px-*,py-*,pt-* - Margin:
m-*,mx-auto, negative margins space-x-*andspace-y-*- Width and height:
w-*,h-*,size-* - Fractions,
full,screen,min-*,max-* - Container and
max-w-*for readable widths
Syntax
html
<div class="p-4 px-6 mt-8 mb-2">Padding and margin</div>
<div class="mx-auto max-w-3xl px-4">Centered container</div>
<div class="flex flex-col space-y-4">
<p>One</p>
<p>Two</p>
</div>
<img class="size-16 rounded-full" src="/avatar.png" alt="" />
<div class="w-1/2 min-h-screen max-w-prose"></div>
<div class="-mt-4">Negative margin</div>
p-4→padding: 1rem;px= left/right,py= top/bottom.mx-autocenters a block that has a width ormax-width.size-16sets width and height together (4rem).w-1/2= 50%,w-full= 100%,w-screen= 100vw,min-h-screen= at least the viewport height.-mt-4flips the sign for negative margins.
Important Properties
| Utility | Purpose | Syntax | Example |
|---|---|---|---|
p-* | Padding | p-{n} | p-6 |
m-* | Margin | m-{n} | mt-4 |
gap-* | Gap in flex/grid | gap-{n} | gap-3 |
space-y-* | Vertical spacing between children | space-y-{n} | space-y-4 |
w-* / h-* | Width / height | w-{n} | w-64 |
size-* | Width and height | size-{n} | size-10 |
max-w-* | Maximum width | max-w-{size} | max-w-xl |
Common Use Cases
- Beginner: add breathing room with
p-4on cards andmt-4between sections. - Practical UI: center page content using
mx-auto max-w-5xl px-4 sm:px-6. - Real-world: make a square avatar and an icon button with
size-10instead of repeatingw-10 h-10.
Common Errors
- ❌
mx-autoon an element with no width → ✅ addmax-w-*orw-*so there is free space to distribute. - ❌ Using
space-y-4on a flex/grid layout with wrapping → ✅ prefergap-4, which works for both axes and wrapped rows. - ❌
w-100expecting 100 pixels → ✅ the number is a scale step, usew-[100px]or a scale value likew-24. - ❌
h-screenon mobile cutting off content behind browser bars → ✅ usemin-h-dvhorh-dvh.
Common Mistakes
- Setting fixed heights where
min-h-*would let content grow. - Mixing many different spacing values instead of sticking to a few scale steps.
- Forgetting that
gapbelongs on the container, not the children. - Using
marginfor separation inside flex/grid whengapis cleaner.
Quick Reference
html
<section class="mx-auto max-w-5xl px-4 py-12">
<div class="grid gap-6 sm:grid-cols-2">
<div class="p-6">Item</div>
<div class="p-6">Item</div>
</div>
</section>