Sub topic 4
The default container size scale
This chapter assumes you can already mark an element with @container and apply a variant such as @md:flex-row to a child. Here you learn what sizes those variant names stand for, so you can pick the right one without guessing.
Variant names are sizes, not breakpoints
A container variant such as @md applies when the nearest @container ancestor is at least a certain width. Tailwind v4 defines that width in a built-in scale. The scale is named after the max-w-* sizes, not after the viewport breakpoints.
This is the most common source of confusion. The viewport variant md: starts at 768px, but the container variant @md: starts at 448px. They share a name and nothing else.
The scale
Each step maps to a width in rem. The pixel values assume a 16px root font size.
| Variant | Minimum container width | Pixels |
|---|---|---|
| `@3xs` | 16rem | 256px |
| `@2xs` | 18rem | 288px |
| `@xs` | 20rem | 320px |
| `@sm` | 24rem | 384px |
| `@md` | 28rem | 448px |
| `@lg` | 32rem | 512px |
| `@xl` | 36rem | 576px |
| `@2xl` | 42rem | 672px |
| `@3xl` | 48rem | 768px |
| `@4xl` | 56rem | 896px |
| `@5xl` | 64rem | 1024px |
| `@6xl` | 72rem | 1152px |
| `@7xl` | 80rem | 1280px |
The scale starts lower than the viewport scale does. Components often live in narrow places such as sidebars, cards, and modals, so the small steps (@3xs to @sm) are the ones you will use most.
Because the sizes use rem, they grow with the user's font size. A user who raises the browser's default font size reaches each step at a larger pixel width.
How the steps behave
Container variants are mobile-first, like viewport variants. An unprefixed class is the base style. A variant such as @md: adds or overrides that style once the container is at least that wide, and it stays active at every larger size.
<div class="@container">
<p class="text-sm @sm:text-base @xl:text-lg">
This text grows with its container.
</p>
</div>Here is what happens as the container widens:
- Below 384px, the text is
text-sm. - From 384px,
@sm:text-basetakes over. - From 576px,
@xl:text-lgtakes over.
The ordering of variants in the class list does not matter. Tailwind sorts them by size in the generated CSS, so the larger step wins when several match.
Seeing the scale in a layout
The next example uses three steps on one grid. The container is allowed to be narrow even on a wide screen, so the layout reacts to the space it has.
<div class="@container">
<div class="grid grid-cols-1 gap-4 @xs:grid-cols-2 @2xl:grid-cols-3">
<div class="rounded bg-white p-4 shadow">One</div>
<div class="rounded bg-white p-4 shadow">Two</div>
<div class="rounded bg-white p-4 shadow">Three</div>
</div>
</div>The grid has one column below 320px, two columns from 320px, and three columns from 672px. If you place this block in a 400px sidebar, it shows two columns. If you place it in a 900px main area, it shows three. The viewport width plays no part.
Adding your own steps
The scale is stored as theme variables named --container-*. You can add a step or change one in your CSS. Customizing the scale is covered in a later chapter, so here you only need to know the scale is not fixed.
Quick reference
- Container sizes are
min-widthconditions on the nearest@containerancestor. - The scale runs from
@3xs(256px) to@7xl(1280px). - Names match
max-w-*sizes, not viewport breakpoints. - Unprefixed classes are the base style, and variants apply at their size and above.
Exercise
A container is 500px wide. Which of these variants are active: @xs, @sm, @md, @lg, @xl?
Show answer
@xs (320px), @sm (384px), and @md (448px) are active, because 500px is at least each of those widths. @lg needs 512px and @xl needs 576px, so they are not active.
Exercise
You want a card to switch from stacked to side by side when its container reaches 672px. Which variant do you use, and what class might you write?
Show answer
Use @2xl, which starts at 42rem (672px). For example: flex flex-col @2xl:flex-row.