Course contentsShow
← Web Development
Lesson 618 of 2,98211. Utility-First CSS with TailwindFree lesson

Spacing: Margin and Padding

Master Tailwind's spacing scale (m-4, p-8, mx-auto) and directional utilities for consistent whitespace.

Spacing: Margin and Padding

You'll learn how to use Tailwind's spacing utilities to control margin and padding with a consistent, predictable scale.

What Is Spacing in Tailwind?

Tailwind provides a numerical spacing scale (0, 1, 2, 3, 4, 8, etc.) that maps to rem values. Instead of writing custom CSS for every margin or padding, you apply utility classes like m-4 (margin) or p-8 (padding). The number represents steps in the scale: m-4 typically equals 1rem (16px), m-8 equals 2rem (32px), and so on.

Core Spacing Utilities

Margin (m) creates space outside an element's border, pushing other elements away. Padding (p) creates space inside the element, between content and border.

You can apply spacing in all directions:

  • m-4 – margin on all sides
  • p-8 – padding on all sides

Directional Control

Target specific sides with directional utilities:

  • mt-4 – margin-top
  • mb-8 – margin-bottom
  • ml-2 – margin-left
  • mr-6 – margin-right
  • mx-4 – horizontal margin (left + right)
  • my-8 – vertical margin (top + bottom)

Same pattern works for padding: pt-4, pb-8, px-2, py-6.

Special Case: Auto Centering

Use mx-auto to horizontally center block elements. This sets margin-left: auto and margin-right: auto, a classic CSS centering technique.

Think of It Like This

Imagine spacing as breathing room. Margin is the personal space around you; padding is the cushioning inside your jacket. Tailwind's numbered scale ensures your breathing room stays consistent across your entire site.

Key Takeaway: Tailwind's spacing utilities (m-*, p-*, mx-*, my-*, and directional variants) give you precise, consistent control over whitespace using a standardized scale, eliminating the need for custom margin and padding declarations.