Day 3 - Containers & Spacing

Control layout width, centering, padding and margin with Tailwind utilities.


1. The container Class

The container class sets max-width to match the current breakpoint. Add mx-auto to center it and px-4 for horizontal padding:

<div class="container mx-auto px-4">
  Your centered content here
</div>
💡 Key Difference from Bootstrap: Bootstrap's .container centers automatically. Tailwind requires mx-auto explicitly. This gives you more control.

2. Max-Width Utilities

Use max-w-{size} to constrain element widths. Combine with mx-auto to center:

max-w-xs (320px)

max-w-xs

max-w-sm (384px)

max-w-sm

max-w-md (448px)

max-w-md

max-w-lg (512px)

max-w-lg

max-w-xl (576px)

max-w-xl

max-w-full (100%)

max-w-full

3. Container Breakpoints

The container class automatically sets max-width at each breakpoint:

Breakpoint Min Width Container Max-Width
none (xs) 0px 100%
sm: 640px 640px
md: 768px 768px
lg: 1024px 1024px
xl: 1280px 1280px
2xl: 1536px 1536px

4. Spacing — Padding & Margin

Tailwind uses shorthand: {property}{sides}-{size}

Properties

  • p = Padding (all sides)
  • m = Margin (all sides)

Sides

  • t = top
  • b = bottom
  • l = left
  • r = right
  • x = horizontal (left + right)
  • y = vertical (top + bottom)

Padding Demo

p-0
p-2
p-4
p-8

Common Spacing Classes

Class CSS Value Pixels
p-0 / m-0 0 0px
p-1 / m-1 0.25rem 4px
p-2 / m-2 0.5rem 8px
p-4 / m-4 1rem 16px
p-6 / m-6 1.5rem 24px
p-8 / m-8 2rem 32px
mx-auto Centers block
← Previous: Buttons Next: Grid Layout →