Container & Columns
Before diving into flexbox and grid, you need two foundational layout tools — the container class that centres and max-widths your page content at responsive breakpoints, and the columns utilities that implement CSS multi-column layout for magazine-style text flows. These are the building blocks every page-level layout starts from.
container class does and
how it differs from max-w-*
mx-auto and adding padding
tailwind.config.js
columns-* utility — column
count and column width
break-inside-avoid — preventing
items from splitting across columns
The container Class
The container class sets a
max-width on an element that matches the current breakpoint.
Unlike max-w-* which sets a fixed maximum,
container is responsive — it grows with the
screen up to each breakpoint's maximum, then stops. It does not centre
itself by default — you add mx-auto for that.
<!-- Basic container — sets max-width but NOT centred by default -->
<div class="container">...</div>
<!-- ✅ Centred container — the pattern you'll use 99% of the time -->
<div class="container mx-auto">...</div>
<!-- ✅ Centred with horizontal padding (recommended) -->
<div class="container mx-auto px-4 sm:px-6 lg:px-8">
Page content here
</div>
<!-- Full page layout — sections share one centred container -->
<header class="bg-white border-b border-gray-100">
<div class="container mx-auto px-4 py-4 flex items-center justify-between">
<span class="font-bold text-xl">Logo</span>
<nav class="flex gap-6">Nav</nav>
</div>
</header>
<main>
<section class="py-20">
<div class="container mx-auto px-4">
Hero content
</div>
</section>
<section class="py-16 bg-gray-50">
<div class="container mx-auto px-4">
Features content
</div>
</section>
</main>
container vs max-w-* — Which to Use?
Both container and
max-w-* constrain width, but they behave
differently. Understanding the distinction lets you choose the right tool for
each situation.
| Feature | container | max-w-{size} |
|---|---|---|
| Width behaviour | Responsive — jumps to each breakpoint's max-width | Fixed maximum at all screen sizes |
| Centring | Needs mx-auto added
manually |
Also needs mx-auto
|
| Values | Matches breakpoint widths (640, 768, 1024, 1280, 1536px) | Any size: sm(384), md(448), lg(512), xl(576), 2xl(672), 3xl(768), 4xl(896), 5xl(1024), 6xl(1152), 7xl(1280) |
| Best for | Full page wrappers, section containers, site-wide layout | Cards, modals, forms, prose content, narrower UI elements |
| Config | Customisable via theme.container in
config |
Customisable via theme.maxWidth |
<!-- Narrow form — centred, max 480px -->
<form class="max-w-sm mx-auto">...</form>
<!-- Card — max 448px -->
<div class="max-w-md mx-auto bg-white rounded-2xl p-8 shadow-lg">...</div>
<!-- Article body — max readable line length -->
<article class="max-w-prose mx-auto px-4">
<p class="leading-relaxed">Article text...</p>
</article>
<!-- Wide content area — max 1152px -->
<section class="max-w-6xl mx-auto px-4">...</section>
<!-- Full image — never wider than parent -->
<img class="max-w-full h-auto" src="..." alt="...">
<!-- No max-width limit — stretches full -->
<div class="max-w-none">...</div>
Customising the Container
By default the container does not centre itself and has no padding. You can change
both behaviours globally in tailwind.config.js
so every container in your project is
automatically centred and padded — without adding mx-auto px-4
every time.
module.exports = {
content: ["./src/**/*.{html,js,jsx,tsx}"],
theme: {
container: {
/* Auto-centre all containers — removes need for mx-auto */
center: true,
/* Add responsive padding to all containers */
padding: {
DEFAULT: '1rem', // 16px on mobile
sm: '1.5rem', // 24px on sm+
lg: '2rem', // 32px on lg+
xl: '2.5rem', // 40px on xl+
'2xl': '3rem', // 48px on 2xl+
},
/* Custom max-widths per breakpoint (override defaults) */
screens: {
sm: '640px',
md: '768px',
lg: '1024px',
xl: '1200px', // ← custom (default is 1280px)
'2xl': '1400px', // ← custom (default is 1536px)
},
},
},
plugins: [],
};
/* With center: true — container is now automatically centred */
<div class="container">Centred & padded automatically</div>
<!-- No need for: class="container mx-auto px-4" -->
center: true and padding in the config is the
recommended approach for production projects — it ensures a consistent page wrapper
without repeating the same classes in every HTML file.
Columns Utilities — CSS Multi-Column Layout
Tailwind's columns-* class implements CSS
multi-column layout — content flows automatically from one column
to the next, like a newspaper. This is different from CSS Grid or Flexbox, which
control the placement of children. Columns control the flow of content.
<!-- ── Column count ─────────────────────────────────── -->
<div class="columns-2 gap-6"> 2 columns with 24px gap
<div class="columns-3 gap-8"> 3 columns with 32px gap
<div class="columns-4 gap-4"> 4 columns with 16px gap
<!-- ── Column width (auto-count based on min width) ─── -->
<div class="columns-xs gap-6"> columns minimum 20rem (320px) wide
<div class="columns-sm gap-6"> columns minimum 24rem (384px) wide
<div class="columns-md gap-6"> columns minimum 28rem (448px) wide
<div class="columns-lg gap-6"> columns minimum 32rem (512px) wide
<div class="columns-xl gap-6"> columns minimum 36rem (576px) wide
<!-- ── Responsive columns ──────────────────────────── -->
<div class="columns-1 sm:columns-2 lg:columns-3 gap-6">
Mobile: 1 col → tablet: 2 → desktop: 3
</div>
<!-- ── Prevent items breaking across columns ─────────── -->
<div class="columns-3 gap-6">
<div class="break-inside-avoid mb-6">Card 1</div>
<div class="break-inside-avoid mb-6">Card 2</div>
<div class="break-inside-avoid mb-6">Card 3</div>
</div>
<!-- ── Column span — element spans all columns ──────── -->
<div class="columns-3 gap-6">
<h2 class="col-span-all text-2xl font-bold mb-4">Full-width heading</h2>
<p class="break-inside-avoid">Column text flows here...</p>
</div>
Column Size Reference
When you pass a named size to columns-*,
Tailwind sets the column-width to that value —
the browser then creates as many columns as fit in the available space. This is
the responsive-by-nature approach to multi-column layout.
| Class | column-width | Approx. columns at 1280px |
|---|---|---|
| columns-1 | column-count: 1 | Always 1 column |
| columns-2 | column-count: 2 | Always 2 columns |
| columns-3 | column-count: 3 | Always 3 columns |
| columns-4 | column-count: 4 | Always 4 columns |
| columns-xs | column-width: 20rem (320px) | ~4 columns |
| columns-sm | column-width: 24rem (384px) | ~3 columns |
| columns-md | column-width: 28rem (448px) | ~2–3 columns |
| columns-lg | column-width: 32rem (512px) | ~2 columns |
| columns-xl | column-width: 36rem (576px) | ~2 columns |
| columns-2xl | column-width: 42rem (672px) | ~1–2 columns |
| columns-3xl | column-width: 48rem (768px) | ~1 column |
| columns-auto | column-count: auto | Browser decides |
Real-World Patterns
Container and columns are used together in many common UI patterns. Here are the most frequently needed layouts you'll build with these utilities.
<!-- Reusable page wrapper pattern -->
<section class="py-16 md:py-24">
<div class="container mx-auto px-4 sm:px-6 lg:px-8">
<!-- Section header -->
<div class="max-w-2xl mx-auto text-center mb-12">
<h2 class="text-3xl md:text-4xl font-bold text-gray-900 dark:text-white mb-4">
Section Title
</h2>
<p class="text-lg text-gray-500 leading-relaxed">
Section description text that is narrower than the full container.
</p>
</div>
<!-- Full-width grid content -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8">
<div class="bg-white dark:bg-gray-900 rounded-2xl p-6 shadow-sm">Card 1</div>
<div class="bg-white dark:bg-gray-900 rounded-2xl p-6 shadow-sm">Card 2</div>
<div class="bg-white dark:bg-gray-900 rounded-2xl p-6 shadow-sm">Card 3</div>
</div>
</div>
</section>
<!-- Masonry layout — cards with varying heights flow naturally -->
<div class="columns-1 sm:columns-2 lg:columns-3 gap-6">
<!-- Each card: break-inside-avoid keeps it in one column -->
<div class="break-inside-avoid mb-6 bg-white dark:bg-gray-900
rounded-2xl border border-gray-100 dark:border-gray-800 p-6 shadow-sm">
<h3 class="font-semibold mb-2">Short Card</h3>
<p class="text-sm text-gray-500">A short card content.</p>
</div>
<div class="break-inside-avoid mb-6 bg-white dark:bg-gray-900
rounded-2xl border border-gray-100 dark:border-gray-800 p-6 shadow-sm">
<h3 class="font-semibold mb-2">Tall Card</h3>
<p class="text-sm text-gray-500">
A taller card with more content that makes it taller than its neighbours.
The multi-column layout flows content naturally.
</p>
</div>
<!-- Add as many cards as you need — they wrap automatically -->
</div>
<!-- Newspaper article layout -->
<article class="max-w-4xl mx-auto px-4 py-12">
<!-- Full-width headline -->
<h1 class="text-4xl font-black text-gray-900 dark:text-white mb-2">
Breaking News Headline
</h1>
<p class="text-gray-500 mb-8">By Author · 2 hours ago</p>
<!-- Multi-column body text -->
<div class="columns-1 md:columns-2 gap-8 text-gray-700 dark:text-gray-300
leading-relaxed text-base">
<p class="mb-4">First paragraph of the article flows into columns...</p>
<!-- Pull quote spans all columns -->
<blockquote class="col-span-all border-l-4 border-cyan-500 pl-6 py-2 my-6
text-xl italic text-gray-900 dark:text-white">
"An important pull quote that spans both columns for emphasis."
</blockquote>
<p class="mb-4">Article continues after the pull quote...</p>
</div>
</article>
<!-- FAQ items in two columns with masonry flow -->
<section class="py-20">
<div class="container mx-auto px-4">
<h2 class="text-3xl font-bold text-center mb-12">Frequently Asked Questions</h2>
<div class="columns-1 md:columns-2 gap-6">
<div class="break-inside-avoid mb-6 p-6 bg-gray-50 dark:bg-gray-900 rounded-xl">
<h3 class="font-semibold text-gray-900 dark:text-white mb-2">Question 1?</h3>
<p class="text-sm text-gray-500">Answer to question one.</p>
</div>
<div class="break-inside-avoid mb-6 p-6 bg-gray-50 dark:bg-gray-900 rounded-xl">
<h3 class="font-semibold text-gray-900 dark:text-white mb-2">Question 2?</h3>
<p class="text-sm text-gray-500">Longer answer to question two that makes this taller.</p>
</div>
</div>
</div>
</section>
break-inside, break-before & break-after
Tailwind provides three sets of break utilities that control how the browser
splits elements across multi-column contexts (and printed pages). The most
important is break-inside-avoid — you'll
use it on every card inside a columns container.
| Class | CSS property | Effect |
|---|---|---|
| break-inside-auto | break-inside: auto | Default — element can break across columns |
| break-inside-avoid | break-inside: avoid | Element stays intact — never split across a column boundary |
| break-inside-avoid-page | break-inside: avoid-page | Don't split across printed pages |
| break-inside-avoid-column | break-inside: avoid-column | Don't split across column boundaries |
| break-before-column | break-before: column | Force element to start at the top of a new column |
| break-before-avoid | break-before: avoid | Avoid column break before this element |
| break-after-column | break-after: column | Force a column break after this element |
| break-after-avoid | break-after: avoid | Avoid column break after this element |
break-inside-avoid to every direct child of a columns container
— whether it's a card, image, or paragraph. Without it, the browser may split a card
awkwardly mid-way through its content across two columns.
- container sets a responsive max-width that matches the current breakpoint.
It does not centre itself — always pair with
mx-autoand add horizontal padding withpx-4 sm:px-6 lg:px-8. - Container max-widths: 100% (default) → 640px (sm) → 768px (md) → 1024px
(lg) → 1280px (xl) → 1536px (2xl). Set
center: trueandpaddingin the config to avoid repeating classes. - max-w-{size} is a fixed maximum — use it for cards, forms, modals, and
prose content.
max-w-prose(65ch) is ideal for article text readability. - columns-{n} sets a fixed column count. columns-{size} sets a minimum column width — the browser creates as many columns as fit. Both use CSS multi-column layout where content flows automatically.
- Always add break-inside-avoid to every direct child of a columns container — cards, images, paragraphs — to prevent the browser splitting them across column boundaries.
- col-span-all makes an element span all columns — perfect for full-width headings and pull quotes inside multi-column text flow.
- Masonry layout is the most popular use case for columns — a
columns-1 sm:columns-2 lg:columns-3 gap-6container withbreak-inside-avoid mb-6on each card creates a Pinterest-style layout without JavaScript. - Next chapter covers Display utilities — block, inline, flex, grid, hidden, and all the display property values Tailwind exposes.
Quick Check — Questions & Answers
Click each question to reveal the detailed answer.
container class only sets max-width — it does not
add margin: 0 auto. This is a deliberate design decision: some layouts
position the container against one edge (like a left-aligned sidebar layout) and do
not want it centred.To centre it, always pair it with
mx-auto:<div class="container mx-auto">If you want all containers to be centred automatically, set
center: true in tailwind.config.js under
theme.container — then you never need to add mx-auto
manually.
columns-3 — sets column-count: 3. The
browser always creates exactly 3 columns regardless of the
available width. On a narrow mobile screen this may create very cramped
columns.columns-sm — sets
column-width: 24rem (384px). The browser creates as many
columns as fit at that minimum width. On a wide desktop it may show 3–4
columns; on a tablet it shows 2; on mobile it collapses to 1. This is inherently
responsive.Recommendation: use
columns-{size} for naturally
responsive layouts, or use columns-{n} with responsive prefixes:
columns-1 sm:columns-2 lg:columns-3.
Fix: Add
break-inside-avoid to every direct child of
the columns container:<div class="break-inside-avoid mb-6">Card content</div>This tells the browser the element must not be split — it will be moved to the start of the next column if it doesn't fit in the current one. Always combine this with
mb-6 (or similar) for spacing between cards since the column gap only
applies between columns, not between rows.
container mx-auto — jumps through Tailwind's
responsive breakpoint widths (640, 768, 1024, 1280, 1536px). The width is
always tied to the breakpoint system. Best for site-wide page wrappers where you
want the layout to change at each breakpoint.max-w-7xl mx-auto (or any other max-w-*)
— sets a fixed maximum of exactly 1280px regardless of which breakpoint
you're at. The element will be narrower than that on smaller screens, but it never
gets wider than 1280px. Best when you want a specific fixed maximum rather than the
system's breakpoint widths.Many teams skip
container entirely and use
max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 — it gives more control over the
exact maximum.
The pattern:
1. Parent:
columns-1 sm:columns-2 lg:columns-3 gap-62. Each child:
break-inside-avoid mb-6The
break-inside-avoid keeps each card intact. The mb-6
adds vertical spacing between cards in the same column (the gap only applies between
columns horizontally).This approach does not require JavaScript (unlike Masonry.js or CSS Grid with complex spanning). It works natively in all modern browsers and is responsive with a single set of responsive prefixes.
MCQ Quiz — Test Yourself
Select the best answer. The correct option is revealed after you click.
container class apply at the
lg breakpoint (≥1024px)?
max-w-*
class sets the ideal text reading line length of 65 characters?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.