Nearby lessons

38 of 61

📏 Tailwind CSS - Margin

📖 Margin Overview

Margin utilities in Tailwind CSS control the space outside an element's border. They are essential for creating proper spacing between elements and managing layout flow.

Unlike padding (internal spacing), margin creates external space around elements, affecting how they interact with neighboring elements.

🎯 All Sides Margin (m)

Use the m- prefix to apply margin to all sides of an element:

  • m-0 - No margin
  • m-1 - 4px margin
  • m-2 - 8px margin
  • m-3 - 12px margin
  • m-4 - 16px margin
  • m-5 - 20px margin
  • m-6 - 24px margin
  • m-7 - 28px margin
  • m-8 - 32px margin
  • m-10 - 40px margin
  • m-12 - 48px margin
  • m-16 - 64px margin
  • m-20 - 80px margin
  • m-24 - 96px margin
  • m-32 - 128px margin
  • m-40 - 160px margin
  • m-48 - 192px margin
  • m-56 - 224px margin
  • m-64 - 256px margin
  • m-72 - 288px margin
  • m-80 - 320px margin
  • m-96 - 384px margin
  • m-auto - Auto margin (centers block elements)
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

⬆️ Top Margin (mt)

Use mt- to apply margin only to the top of an element:

  • mt-0 - No top margin
  • mt-1 - 4px top margin
  • mt-2 - 8px top margin
  • mt-4 - 16px top margin
  • mt-8 - 32px top margin
  • mt-16 - 64px top margin
  • mt-auto - Auto top margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

➡️ Right Margin (mr)

Use mr- to apply margin only to the right side of an element:

  • mr-0 - No right margin
  • mr-2 - 8px right margin
  • mr-4 - 16px right margin
  • mr-8 - 32px right margin
  • mr-16 - 64px right margin
  • mr-auto - Auto right margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

⬇️ Bottom Margin (mb)

Use mb- to apply margin only to the bottom of an element:

  • mb-0 - No bottom margin
  • mb-2 - 8px bottom margin
  • mb-4 - 16px bottom margin
  • mb-8 - 32px bottom margin
  • mb-16 - 64px bottom margin
  • mb-auto - Auto bottom margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

⬅️ Left Margin (ml)

Use ml- to apply margin only to the left side of an element:

  • ml-0 - No left margin
  • ml-2 - 8px left margin
  • ml-4 - 16px left margin
  • ml-8 - 32px left margin
  • ml-16 - 64px left margin
  • ml-auto - Auto left margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

↔️ Horizontal Margin (mx)

Use mx- to apply margin to both left and right sides (horizontal):

  • mx-0 - No horizontal margin
  • mx-2 - 8px horizontal margin
  • mx-4 - 16px horizontal margin
  • mx-6 - 24px horizontal margin
  • mx-8 - 32px horizontal margin
  • mx-12 - 48px horizontal margin
  • mx-16 - 64px horizontal margin
  • mx-auto - Auto horizontal margin (centers block elements)
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

↕️ Vertical Margin (my)

Use my- to apply margin to both top and bottom sides (vertical):

  • my-0 - No vertical margin
  • my-2 - 8px vertical margin
  • my-4 - 16px vertical margin
  • my-6 - 24px vertical margin
  • my-8 - 32px vertical margin
  • my-12 - 48px vertical margin
  • my-16 - 64px vertical margin
  • my-auto - Auto vertical margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

🎯 Auto Margin for Centering

Use auto margin to center block-level elements:

  • mx-auto - Centers horizontally (requires width constraint)
  • my-auto - Centers vertically (requires flexbox or grid)
  • m-auto - Centers both horizontally and vertically
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

🔢 Arbitrary Values

Use square brackets to apply any custom margin value:

  • m-[10px] - Custom 10px margin
  • m-[2.5rem] - Custom 2.5rem margin
  • mx-[15%] - Custom 15% horizontal margin
  • my-[1.2em] - Custom 1.2em vertical margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

📱 Responsive Margin

Combine margin utilities with responsive prefixes:

  • m-4 md:m-8 - 16px margin, 32px on medium screens
  • mx-2 lg:mx-12 - 8px horizontal, 48px on large screens
  • my-4 sm:my-6 xl:my-12 - Responsive vertical margin
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

⚠️ Margin Collapse

Understanding margin collapse behavior:

  • Adjacent margins: Vertical margins of adjacent elements collapse
  • Parent-child margins: Top/bottom margins can collapse with parent
  • Flexbox/Grid: Margins don't collapse in flexbox or grid
  • Solution: Use padding instead of margin when needed
🌀

tailwind Designer

Live Component Playground

html Source
Loading...
Built with Tailwind CSS • Instant Reflection

✅ Margin Best Practices

  • Use margin for external spacing: Space between elements
  • Use padding for internal spacing: Space inside elements
  • Be consistent: Stick to Tailwind's spacing scale
  • Consider responsive design: Adjust margins for different screens
  • Use auto margin for centering: mx-auto for horizontal centering
  • Watch for margin collapse: Be aware of vertical margin behavior
  • Combine with flexbox/grid: Prevent margin collapse when needed
  • Use negative margins carefully: For overlapping effects

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Tailwind CSS - Space Between