Spacing
Spacing scale and usage conventions.
Scale
Padding vs. Margin
Padding is the space inside an element, between its border and its content. It pushes content inward and is part of the element's clickable/visible area. Use padding to create breathing room within a component.
Margin is the space outside an element, between its border and its neighbours. It pushes adjacent elements away. Use margin to create separation between components.
A good rule of thumb: use padding for internal component spacing (how much room content has inside a card, button, or section) and margin for external layout spacing (how far apart two components sit from each other). In flex/grid layouts, prefer gap over margins — it avoids the double-spacing problem at edges.
{/* Padding — space inside the card */} <div class="p-6 border rounded-lg">Card content</div> {/* Margin — space between cards */} <div class="mt-8">Next section</div> {/* Gap — preferred for flex/grid children */} <div class="flex gap-4"> <div>Item 1</div> <div>Item 2</div> </div>
Directional spacing
Tailwind provides directional variants for both padding and margin. Use these to control spacing on specific axes.
Vertical (top/bottom)
Use py-* for symmetric vertical padding, or pt-* / pb-* to target one side. For margin, use my-*, mt-*, mb-*.
{/* Symmetric vertical padding */} <section class="py-12">Equal top and bottom</section> {/* Asymmetric — more space above */} <section class="pt-16 pb-8">More breathing room above</section> {/* Vertical margin between sections */} <div class="my-10">Spaced equally from siblings</div>
Horizontal (left/right)
Use px-* for symmetric horizontal padding, or pl-* / pr-* to target one side. For margin, use mx-*, ml-*, mr-*. For centering, mx-auto is the standard pattern.
{/* Symmetric horizontal padding */} <div class="px-6">Content with side padding</div> {/* Centered container */} <div class="mx-auto max-w-5xl px-8">Centered content</div> {/* RTL-safe: use logical properties */} <div class="ps-4 pe-2">Start/end padding (flips for RTL)</div>
For layouts that must support RTL (Arabic), use logical properties: ps-* / pe-* (padding-inline-start/end) and ms-* / me-* (margin-inline-start/end) instead of pl-* / pr-*. These flip automatically when the page direction changes.
Guidelines
- Use the scale — avoid arbitrary pixel values. Consistent spacing creates visual rhythm and makes responsive adjustments predictable.
- Prefer
gap— when spacing flex or grid children, usegap-*instead of adding margins to individual items. - Component padding — most components use
p-3orp-4for internal padding. Cards and panels typically usep-6. - Section spacing — vertical spacing between page sections uses
py-12topy-20depending on visual weight.
When in doubt, pick the next value up from the scale rather than the next value down. Generous whitespace improves readability.