Sub topic 4
The default container size scale
Earlier chapters showed that a container variant such as @md reads the width of the nearest @container ancestor. This chapter covers the scale behind those names: which widths they stand for, how they differ from the viewport breakpoints you already know, and what the scale shares with other utilities.
This chapter assumes you can read the examples from the previous chapters and know the viewport variants sm:, md: and lg:. The values below are for Tailwind v4, where container queries are built in. The v3 plugin is covered in a later chapter.
What the scale is
Tailwind ships a fixed list of named container sizes. Each name maps to a width in rem. A container variant such as @md: applies when the container is at least that wide.
| Variant | Theme variable | Width in rem | Width at 16px root |
|---|---|---|---|
| `@3xs` | `--container-3xs` | 16rem | 256px |
| `@2xs` | `--container-2xs` | 18rem | 288px |
| `@xs` | `--container-xs` | 20rem | 320px |
| `@sm` | `--container-sm` | 24rem | 384px |
| `@md` | `--container-md` | 28rem | 448px |
| `@lg` | `--container-lg` | 32rem | 512px |
| `@xl` | `--container-xl` | 36rem | 576px |
| `@2xl` | `--container-2xl` | 42rem | 672px |
| `@3xl` | `--container-3xl` | 48rem | 768px |
| `@4xl` | `--container-4xl` | 56rem | 896px |
| `@5xl` | `--container-5xl` | 64rem | 1024px |
| `@6xl` | `--container-6xl` | 72rem | 1152px |
| `@7xl` | `--container-7xl` | 80rem | 1280px |
There are thirteen steps. The names run from 3xs to 7xl, and the widths grow in rem steps of 2 to 8.
Container sizes are not viewport breakpoints
The container names look like the viewport names, but the widths are different. This is the most common source of confusion.
| Name | Viewport variant (min width) | Container variant (min width) |
|---|---|---|
| `sm` | 40rem (640px) | 24rem (384px) |
| `md` | 48rem (768px) | 28rem (448px) |
| `lg` | 64rem (1024px) | 32rem (512px) |
| `xl` | 80rem (1280px) | 36rem (576px) |
| `2xl` | 96rem (1536px) | 42rem (672px) |
The viewport scale describes whole screens. The container scale describes components, which are usually much narrower than a screen. A card in a sidebar might be 300px wide on a 1440px monitor. The container scale is therefore tuned for widths between 256px and 1280px.
A practical consequence: do not translate md: to @md: and expect the same switch point. @md fires at 448px of container width, while md: fires at 768px of viewport width. Pick the container variant by asking how wide the component needs to be before the layout change makes sense.
What a variant compiles to
A container variant is a minimum-width test. Tailwind compiles @md:flex-row roughly to this:
@container (width >= 28rem) {
.\@md\:flex-row {
flex-direction: row;
}
}The number 28rem comes straight from --container-md. The condition is >=, so a container that is exactly 28rem wide matches @md. A container one pixel narrower does not.
Three details follow from this.
- Mobile first, like viewport variants. An unprefixed class is the base style. Each larger variant overrides it once the container is wide enough. Tailwind sorts container variants from small to large in the generated CSS, so when several match, the largest one wins.
- The measured width is the container's content box. Padding and borders on the
@containerelement do not count. If you putp-8on a container that is 480px wide, the query sees 416px, and@lg(512px) does not apply either way, but@md(448px) would also fail. When a variant "fires late", check the padding on the container. - The units are rem, not px. The pixel column above assumes a 16px root font size. If a user raises the browser's default font size to 20px,
@mdbecomes 560px. Viewport media queries written in rem behave the same way, so layouts adapt to larger text instead of breaking.
Seeing the scale at work
This example shows which step a container has reached by changing the background colour. Resize the parent, not the window, to see it. The resize-x overflow-hidden classes make the outer box draggable in most browsers.
<div class="@container resize-x overflow-hidden border w-96 max-w-full">
<div class="bg-gray-200 p-3 text-sm @xs:bg-green-200 @md:bg-blue-200 @xl:bg-purple-200">
<span class="@max-xs:inline hidden">Under @xs (below 320px)</span>
<span class="@xs:@max-md:inline hidden">@xs up to @md (320px to 447px)</span>
<span class="@md:@max-xl:inline hidden">@md up to @xl (448px to 575px)</span>
<span class="@xl:inline hidden">@xl and wider (576px and up)</span>
</div>
</div>Here w-96 is 24rem (384px), so the box starts in the green band and shows the second label. Drag it wider than 448px for blue, and wider than 576px for purple. The @xs:@max-md: stack is a range, which the next chapter explains in detail. Only one label is visible at a time.
Note that w-96 and max-w-full are on the container itself. The container needs a width that does not depend on its own contents, or the query has nothing stable to measure. See the pitfalls chapter for details.
Choosing steps for a layout
Many components need only two or three switch points. These are the usual uses of each range:
@3xsto@xs(256 to 320px): very narrow slots such as a compact sidebar widget. Use these to drop secondary text or collapse an icon row.@smto@lg(384 to 512px): the typical card width. The switch from a stacked layout to an image-beside-text layout most often lands at@mdor@lg.@xlto@3xl(576 to 768px): content panels, form groups, and multi-column grids of small items.@4xlto@7xl(896 to 1280px): large regions like a main content area, where you might add a second or third column.
A common rule is to find the narrowest width at which your wider layout still looks good, then choose the nearest step at or above it. If a row layout needs 420px, use @md (448px), not @sm (384px), because the row would be cramped between 384px and 420px.
<div class="@container">
<div class="grid grid-cols-1 gap-4 @lg:grid-cols-2 @5xl:grid-cols-3">
<div class="rounded bg-white p-4 shadow">Panel A</div>
<div class="rounded bg-white p-4 shadow">Panel B</div>
<div class="rounded bg-white p-4 shadow">Panel C</div>
</div>
</div>This grid has one column below 512px, two from 512px, and three from 1024px of container width. Place it in a narrow column and it stays at one column, even on a very wide screen.
One scale, two jobs
The --container-* theme variables are not exclusive to container queries. The same namespace also feeds the max-w-* utilities, so max-w-md is max-width: 28rem and @md is (width >= 28rem). Both read --container-md.
This explains why the names run from 3xs to 7xl and why they match the Tailwind max-width scale you may already know. It also has a side effect when you customize. Adding --container-8xl: 96rem; gives you the @8xl variant and also a max-w-8xl utility. Removing or resetting sizes affects both.
@import "tailwindcss";
@theme {
--container-*: initial;
--container-card: 26rem;
--container-panel: 40rem;
}After this change, @card and @panel exist as variants, and @md no longer does. Class names such as @md:flex-row generate no CSS, and max-w-md stops working as well. Reset the namespace only when you intend to replace the whole scale. To add a single size, declare just that variable and keep the defaults. The theme chapter later in this tutorial covers customization in full.
Relating the scale to `@max-*` and `@min-*`
Every name in the table also works with @min- and @max-:
@min-md:means the same as@md:, that is,(width >= 28rem).@max-md:means(width < 28rem). The boundary is exclusive, so a container exactly 28rem wide does not match@max-md.
Because @md and @max-md split at the same width, they never overlap and never leave a gap. That makes a pair like @max-md:flex-col @md:flex-row safe, although a base class plus @md:flex-row is usually shorter.
Common mistakes with the scale
- Copying viewport breakpoints. Writing
@md:and expecting 768px. It is 448px. Check the table when a layout switches earlier than you expected. - Forgetting the container's padding. The query measures the content box, so horizontal padding on the
@containerelement reduces the width the variants see. - Using a name that does not exist.
@xsexists, but@4xsdoes not. A class with an unknown size generates nothing, and the element silently stays unstyled. Check the spelling against the table. - Assuming pixel values. A user with a larger default font size gets larger switch points. Test your component with a bigger root font size once.
- Resetting
--container-*by accident. This removesmax-w-*sizes along with the variants.
Exercise
A card sits in a container that is 440px wide. The card has the classes flex flex-col @md:flex-row @max-lg:gap-2 @lg:gap-6, and the root font size is 16px. Which of @md and @lg match, and what layout and gap does the card get?
Show answer
Neither @md (28rem, 448px) nor @lg (32rem, 512px) matches, because 440px is below 448px. The card stays in the base flex-col layout. @max-lg does match, because 440px is below 512px, so the gap is gap-2. The result is a stacked card with a small gap.
Exercise
A teammate changed a component from md:grid-cols-2 to @md:grid-cols-2. The component sits in a 600px-wide column on a 1400px-wide screen. Before the change, did the grid have two columns? After the change? Explain the difference.
Show answer
Before, md: checks the viewport (768px minimum). The viewport is 1400px, so it matched and the grid had two columns. After, @md: checks the nearest container (448px minimum). The column is 600px wide, which is at least 448px, so it still has two columns, provided the column's content width, after padding, is still at least 448px and a @container ancestor exists. The two rules differ when the column is narrow, for example 400px: the md: version would still show two columns on a wide screen, while the @md: version would show one.
Exercise
You add --container-9xl: 112rem; to your @theme block. Name two new classes you can now use, and say what happens to @md: and max-w-md.
Show answer
You can use the variant @9xl: (for example @9xl:grid-cols-4) and the utility max-w-9xl. Nothing happens to @md: or max-w-md. Adding a variable extends the scale, and only a reset such as --container-*: initial; would remove the defaults.