Skip to content

Tailwind Container

The container class is Tailwind's built-in way to center page content and constrain its maximum width at each breakpoint. This lesson covers how it works, how to configure it, and common alternatives.

What Is the Container Class?

The container utility sets an element's max-width to match the current breakpoint and, by default, centers it horizontally with margin-inline: auto. It's the Tailwind equivalent of Bootstrap's .container.

Unlike Bootstrap, Tailwind's container does not add horizontal padding by default in a fresh v3/v4 project unless you configure it, so many teams pair container with px-4 or configure padding in tailwind.config.js.

<div class="container mx-auto px-4">
  <h1 class="text-3xl font-bold">Page Content</h1>
  <p>This content is centered and constrained to a readable max width.</p>
</div>

container sets the responsive max-width, mx-auto centers it, and px-4 adds breathing room on small screens.

Container Configuration Syntax

// tailwind.config.js
theme: {
  container: {
    center: true,
    padding: "1rem",
    screens: { lg: "1024px" },
  },
},
  • center: true automatically adds margin-inline: auto so you don't need mx-auto.
  • padding sets default horizontal padding, either a single value or per-breakpoint object.
  • screens can override the max-width used at each breakpoint specifically for the container.
  • Without configuration, container only sets width: 100% plus breakpoint-specific max-width values.

Container Cheatsheet

Default container max-widths and common configuration options.

Breakpoint Default max-width Notes
(base) 100% Full width until the first breakpoint
sm (640px) 640px Matches the sm breakpoint value
md (768px) 768px Matches the md breakpoint value
lg (1024px) 1024px Matches the lg breakpoint value
xl (1280px) 1280px Matches the xl breakpoint value
2xl (1536px) 1536px Matches the 2xl breakpoint value
Centering mx-auto or center: true Required to horizontally center the container
Padding px-4 or container.padding Container has no padding by default

container vs max-w-* Utilities

container snaps its max-width to breakpoint values automatically. max-w-* utilities (like max-w-3xl) let you pick an exact, fixed max-width regardless of screen size, which is common for text-heavy content like articles or forms.

<!-- Breakpoint-based container -->
<div class="container mx-auto px-4">...</div>

<!-- Fixed reading-width wrapper -->
<article class="max-w-2xl mx-auto px-4">...</article>

Use container for full page sections, and max-w-* for narrower content like articles or forms.

Building a Custom Centered Wrapper

Many teams skip the container utility entirely and standardize on a single max-w-* value with mx-auto and consistent padding across the whole site for a simpler mental model.

<div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
  Page content with responsive side padding.
</div>

This pattern is common in official Tailwind UI examples and scales padding at sm and lg breakpoints.

Common Mistakes

  • Forgetting mx-auto, which leaves the container full width but not actually centered.
  • Assuming container adds padding automatically; by default it does not unless configured.
  • Using container for narrow content like a login form, where a fixed max-w-sm is more appropriate.
  • Not configuring container.padding and instead adding px-4 inconsistently across different pages.
  • Nesting multiple container elements, which has no additional effect and adds confusion.

Key Takeaways

  • The container utility sets a responsive max-width matching the current breakpoint.
  • You must add mx-auto (or configure center: true) to actually center the container.
  • Container padding is not automatic by default; configure it or add px-* manually.
  • max-w-* utilities are often a simpler, more flexible alternative for content-focused sections.
  • Consistent container/padding conventions across a project keep spacing predictable.

Pro Tip

Configure container.center: true and a sensible container.padding once in tailwind.config.js so every page automatically gets centered, padded content without repeating mx-auto px-4 everywhere.