0PricingLogin
Tailwind CSS Academy · Lesson

Margin Utilities and Auto Margins

Add space outside elements with m-* utilities, use mx-auto for horizontal centering, and chain directional margins.

Margin vs Padding Revisited

Margin creates space outside an element, between it and adjacent elements. Unlike padding, margin does not add to the element's clickable area or background extent. Margins are transparent — they show whatever is behind them. Use margin to separate components (cards, sections, paragraphs) from each other, and padding to create space within a component.

<div class="bg-blue-100 p-4 mb-4 rounded">
  Card 1 — mb-4 creates 16px below this card
</div>
<div class="bg-blue-100 p-4 rounded">
  Card 2 — pushed down by Card 1's margin
</div>

Uniform Margin: m-*

m-* applies equal margin on all four sides. m-0 is commonly used to override default browser margins — browsers add default margin to elements like <body>, <h1>, and <p>. Tailwind's Preflight CSS reset removes these, but m-0 remains useful when third-party components introduce unexpected margins.

<!-- Third-party component with default margin -->
<ThirdPartyWidget class="m-0" />

<!-- Adding space around all sides -->
<div class="m-4 bg-white rounded shadow p-4">
  Floated card with all-side margin
</div>

All lessons in this course

  1. The Tailwind Spacing Scale
  2. Padding Utilities
  3. Margin Utilities and Auto Margins
  4. Negative Margins and Space Between
← Back to Tailwind CSS Academy