Skip to main content
Tailwind CSS Mastery·Lesson 2 of 5

Layout & Spacing

Good layout is the foundation of every UI. Tailwind gives you a complete set of utilities for controlling size, spacing, and arrangement without writing a single line of custom CSS.

Container

The container class sets max-width to match the current breakpoint. Pair it with mx-auto and horizontal padding for a standard page wrapper:

<div class="container mx-auto px-4 lg:px-8">
  <!-- Page content -->
</div>

You can configure the container to auto-center and add default padding in your Tailwind config so you only need class="container".

Spacing Scale

Tailwind's spacing scale is used by padding (p), margin (m), gap (gap), and space-between (space-x/y) utilities. The default scale maps numbers to rem values:

ClassValue
00
10.25rem (4px)
20.5rem (8px)
41rem (16px)
61.5rem (24px)
82rem (32px)
123rem (48px)
164rem (64px)

Padding & Margin

Ctrl+Enter
HTML
CSS
JS
Preview

Space Between

Use space-x-* or space-y-* to add consistent spacing between child elements without adding classes to each one:

Ctrl+Enter
HTML
CSS
JS
Preview

Width & Height

Ctrl+Enter
HTML
CSS
JS
Preview

Flexbox Layout

Tailwind makes flexbox layouts trivial:

Ctrl+Enter
HTML
CSS
JS
Preview

Key flex utilities: flex, flex-col, flex-wrap, items-center, items-start, items-end, justify-between, justify-center, gap-*, flex-1, shrink-0, grow.

CSS Grid Layout

Grid is the right tool for two-dimensional layouts:

Ctrl+Enter
HTML
CSS
JS
Preview

Practical Example: Pricing Card

Combining layout and spacing into a real component:

Ctrl+Enter
HTML
CSS
JS
Preview