Skip to main content

Tailwind CSS Container Cheatsheet

Complete Tailwind v4 container reference with visual examples.
Click any class to copy to clipboard.

Container Class

ClassCSS / Behavior
containerwidth: 100%; (with max-width at breakpoints)
sm:containermax-width: 640px (at sm breakpoint)
md:containermax-width: 768px (at md breakpoint)
lg:containermax-width: 1024px (at lg breakpoint)
xl:containermax-width: 1280px (at xl breakpoint)
2xl:containermax-width: 1536px (at 2xl breakpoint)

Container Class - Visual Example

The container class sets a max-width that matches the current breakpoint.

container mx-auto

Centered container with max-width

Without container

Full width element

Container Max-Width at Each Breakpoint

BreakpointMin WidthContainer Max-Width
Default0px100%
sm640px640px
md768px768px
lg1024px1024px
xl1280px1280px
2xl1536px1536px

Centering & Padding

Common patterns for centering containers and adding consistent padding.

container mx-auto px-4

Centered with horizontal padding

max-w-screen-lg mx-auto px-6

Fixed max-width with padding

Customizing Container in Config

You can customize the container class behavior in your Tailwind config to automatically center it, add padding, or set custom max-widths.

tailwind.config.js
// tailwind.config.js
module.exports = {
  theme: {
    container: {
      center: true,
      padding: '1rem',
      screens: {
        sm: '640px',
        md: '768px',
        lg: '1024px',
        xl: '1280px',
        '2xl': '1400px', // Custom max-width
      },
    },
  },
}

center: true - Automatically applies mx-auto

padding - Adds horizontal padding to the container

screens - Override the default max-widths at each breakpoint

Container Query Setup (v3.4+)

ClassCSS / Behavior
@containercontainer-type: inline-size;
@container/namecontainer-type: inline-size; container-name: name;
@container-normalcontainer-type: normal;

Container Query Size Reference

These prefixes apply styles based on the container's size, not the viewport.

PrefixMin Container WidthCSS
@xs:20rem (320px)@container (min-width: 20rem)
@sm:24rem (384px)@container (min-width: 24rem)
@md:28rem (448px)@container (min-width: 28rem)
@lg:32rem (512px)@container (min-width: 32rem)
@xl:36rem (576px)@container (min-width: 36rem)
@2xl:42rem (672px)@container (min-width: 42rem)
@3xl:48rem (768px)@container (min-width: 48rem)
@4xl:56rem (896px)@container (min-width: 56rem)
@5xl:64rem (1024px)@container (min-width: 64rem)
@6xl:72rem (1152px)@container (min-width: 72rem)
@7xl:80rem (1280px)@container (min-width: 80rem)

Container Queries - Visual Example

Container queries let child elements respond to their container's size, not the viewport. Resize the container below to see the effect.

@container parent with responsive children

Card 1
Card 2
Card 3

Drag the right edge to resize this container

Common Patterns

Basic Page Layout

<div class="container mx-auto px-4">
  <header>...</header>
  <main>...</main>
  <footer>...</footer>
</div>

Full-Width Section with Contained Content

<section class="bg-gray-100 py-16">
  <div class="container mx-auto px-4">
    <!-- Content is contained -->
  </div>
</section>

Container Query Card

<div class="@container">
  <div class="flex flex-col @md:flex-row gap-4">
    <img class="w-full @md:w-1/3" />
    <div class="@md:w-2/3">
      <h2 class="text-lg @lg:text-2xl">
        Title
      </h2>
    </div>
  </div>
</div>

Named Container Query

<div class="@container/sidebar">
  <nav class="@md/sidebar:flex-col">
    <a>Home</a>
    <a>About</a>
  </nav>
</div>

Container vs Container Queries

Container class (container): Sets a responsive max-width based on the viewport. Use for page-level layout.

Container queries (@container): Allow child elements to style based on their parent's size, not the viewport. Use for reusable components that need to adapt to their context.

Container queries were added in Tailwind CSS v3.4 and are perfect for building truly responsive components that work in any layout context.