Sub topic 3
Styling children with @sm, @md and @lg
This chapter assumes you can already write flexbox and grid layouts with Tailwind utilities. It also assumes you know how viewport variants such as md: and lg: work, and that you have marked an element with @container. The examples use Tailwind v4 syntax. The v3 setup is covered in a later chapter.
What a container variant does
A container variant is a prefix that starts with @. It works like a viewport variant, except that it compares against the width of a parent element instead of the width of the browser window.
<div class="@container">
<div class="flex flex-col gap-4 @md:flex-row">
<img class="w-full @md:w-48" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="text-lg font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description of the product.</p>
</div>
</div>
</div>The outer div is the query container. Its child stacks vertically by default. Once the container is at least @md wide, the child switches to a row and the image shrinks to w-48. The window width is irrelevant. If this markup sits in a 20rem sidebar on a 4K monitor, it stays stacked.
Tailwind compiles @md:flex-row to roughly this CSS:
.\@md\:flex-row {
@container (width >= 28rem) {
flex-direction: row;
}
}The @container at-rule is native CSS. Tailwind adds no JavaScript and no runtime. It generates the rules and the browser evaluates them.
The three rules that govern every container variant
- A container variant needs an ancestor marked with
@container. The@containerclass appliescontainer-type: inline-sizeto that element. Without it, the@md:classes compile but never match. - The query targets the nearest marked ancestor. The browser walks up from the styled element and uses the first ancestor that is a query container. Named containers, covered in a later chapter, change this.
- An element cannot respond to its own size. Variants apply to descendants of the container only. If you put
@containerand@md:flex-rowon the same element, the@md:class looks for a container above it, not at itself.
The third rule is why the examples use two nested elements. The outer one is measured, and the inner one is styled.
<!-- Wrong: the element is not its own container -->
<div class="@container flex flex-col @md:flex-row">...</div>
<!-- Right: measure the parent, style the child -->
<div class="@container">
<div class="flex flex-col @md:flex-row">...</div>
</div>Mobile-first, like viewport variants
Container variants follow the same mobile-first logic as md: and lg:. An unprefixed utility is the base style. A prefixed utility applies at that container width and above, and it overrides the base.
In flex-col @md:flex-row, the container is narrower than @md for the first state and at least @md for the second. The stacking order of the generated CSS makes larger variants win over smaller ones, so you can layer several of them on one element.
<div class="@container">
<div class="grid grid-cols-1 gap-4 @sm:grid-cols-2 @2xl:grid-cols-4">
<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 class="rounded bg-white p-4 shadow">Four</div>
</div>
</div>The grid has one column by default, two columns from @sm (24rem), and four columns from @2xl (42rem). Between those widths, the highest matching variant decides. At a container width of 30rem, @sm matches and @2xl does not, so the grid has two columns.
The table below shows the result for several container widths.
| Container width | Variants that match | Columns |
|---|---|---|
| 20rem | none | 1 |
| 24rem | `@sm` | 2 |
| 40rem | `@sm` | 2 |
| 42rem | `@sm`, `@2xl` | 4 |
How the browser picks the answer
When something does not respond, start at the first decision. In most failures, the @container class is missing or sits on the wrong element.
The sizes behind the names
The names @sm, @md and @lg map to fixed container widths. They are not the same numbers as the viewport breakpoints of sm:, md: and lg:, and they are much smaller. These are the values for the three sizes this chapter uses, with the next two for context.
| Variant | Minimum container width | In pixels at 16px per rem |
|---|---|---|
| `@sm` | 24rem | 384px |
| `@md` | 28rem | 448px |
| `@lg` | 32rem | 512px |
| `@xl` | 36rem | 576px |
| `@2xl` | 42rem | 672px |
A common mistake is to carry over viewport habits and expect @md to mean 768px. A container is usually much narrower than the window, so the container scale starts lower. The full scale is the subject of the next chapter.
Combining container variants with other variants
A container variant is an ordinary Tailwind variant, so it stacks with state variants, dark mode, and viewport variants. Variants apply from left to right.
<div class="@container">
<button
class="rounded bg-blue-600 px-3 py-1 text-sm text-white
hover:bg-blue-700
@md:px-5 @md:py-2 @md:text-base
@md:hover:bg-blue-800
dark:bg-blue-500">
Buy now
</button>
</div>This button gets larger padding and text when its container reaches @md. It also uses a darker hover color at that size. The dark: variant and the container variants do not interfere with each other.
You can also mix viewport and container variants in one class list. This is useful when the page shell depends on the window and the component inside depends on its slot.
<main class="grid grid-cols-1 lg:grid-cols-[18rem_1fr]">
<aside class="@container p-4">
<div class="flex flex-col gap-2 @sm:flex-row">
<span>Filter</span>
<span>Sort</span>
</div>
</aside>
<section class="@container p-4">
<div class="grid gap-4 @lg:grid-cols-2">
<article>First</article>
<article>Second</article>
</div>
</section>
</main>Here lg: decides whether the page has one column or two. Each region then decides its own inner layout from its own width. The sidebar is 18rem wide when the viewport is large, so its inner content stays in a column.
One component in two places
The main benefit shows up when the same markup appears in places of different width. Put this card in a narrow wrapper and a wide wrapper:
<div class="w-72">
<div class="@container">
<div class="flex flex-col gap-3 rounded border p-3 @md:flex-row @md:items-center">
<img class="w-full rounded @md:w-32" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description.</p>
</div>
</div>
</div>
</div>
<div class="w-[40rem]">
<div class="@container">
<div class="flex flex-col gap-3 rounded border p-3 @md:flex-row @md:items-center">
<img class="w-full rounded @md:w-32" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description.</p>
</div>
</div>
</div>
</div>The first wrapper is 18rem wide, below @md at 28rem, so the card stacks. The second is 40rem wide, so the card uses a row. The two cards have identical markup. With md: viewport variants, both cards would switch together, and you would need extra wrapper-specific classes or props to make them differ.
Where to put @container
Place @container on the element whose width reflects the space the component has been given. A few practical choices:
- Wrap the component itself. Put
@containeron a wrapper inside the component, as above. The component then works anywhere without help from its parent. - Mark a layout region. Put
@containeron a column, sidebar, or panel, and let many children respond to it. - Avoid marking every element. Each container adds containment. Mark only the elements that children query.
Wrapping inside the component is the safest default, because the component no longer depends on how a page author sets up the parent. The tradeoff is one extra element in the DOM.
Things that surprise people
The @container class sets container-type: inline-size. That value tells the browser to compute the element's width without looking at its children. This causes two effects you should know.
First, a container cannot size itself to its content in the inline direction. A marked element in a shrink-to-fit context, such as an inline-block or a flex item with no set width, can collapse to zero width. Give it a width, or let it fill its parent as a normal block does.
Second, the query measures only width. Container variants in this chapter respond to the inline size, not the height.
<!-- Can collapse: a flex item with no width, sized by content -->
<div class="flex">
<div class="@container">
<div class="@md:flex-row">...</div>
</div>
</div>
<!-- Safer: let the container grow to fill the row -->
<div class="flex">
<div class="@container flex-1">
<div class="flex flex-col @md:flex-row">...</div>
</div>
</div>Choosing between viewport and container variants
| Situation | Better choice |
|---|---|
| Page-level navigation, header, overall columns | Viewport (`md:`, `lg:`) |
| A card or widget reused in several layouts | Container (`@md:`, `@lg:`) |
| A component inside a resizable panel | Container |
| Hiding something based on device size | Viewport |
Use viewport variants when the question is about the screen. Use container variants when the question is about the room a component has.
Exercise
A card has the classes @container flex flex-col @md:flex-row on a single div. It stays in a column even inside a 40rem wide wrapper. Why, and how do you fix it?
Show answer
A container variant styles descendants of a container, and an element cannot query its own size. The @md:flex-row class looks for a marked ancestor above the div, finds none, and never applies. Move @container to a wrapper around the card: <div class="@container"><div class="flex flex-col @md:flex-row">...</div></div>. The card then responds to the wrapper's width.
Exercise
A container is 30rem wide. Which columns does grid-cols-1 @sm:grid-cols-2 @2xl:grid-cols-4 produce, and why?
Show answer
Two columns. At 30rem, @sm (24rem) matches and @2xl (42rem) does not. The @sm rule overrides the base grid-cols-1, and the @2xl rule is not active.