Sub topic 11
Component-level responsiveness with container queries
This chapter assumes you know the default breakpoints, the mobile-first model, and how md: and max-lg: prefixes work. It also assumes you can read a basic CSS media query. The examples use Tailwind v4, where container queries are built in. A note on v3 appears near the end.
Why viewport breakpoints are sometimes the wrong measure
Breakpoint prefixes such as md: compile to media queries. A media query reads the width of the viewport, which is the browser window. That works when a component's space depends only on the window. It breaks when the same component is reused in places of different widths.
Suppose you build a product card with an image beside the text. At a 1200px viewport, you might place that card in a full-width grid, a two-column grid, or a 300px sidebar. A viewport rule such as md:flex-row treats all three the same, because the window is 1200px in each case. In the sidebar the card is cramped, and in the full-width grid it is fine.
A container query solves this by reading the width of a parent element instead of the window. The card responds to the space it was given, wherever it is placed.
The two-step model
Container queries in Tailwind need two parts.
- Mark an ancestor as a query container with the
@containerclass. - Use container variants such as
@md:on descendants. These variants compare against the nearest marked ancestor's width.
<div class="@container">
<div class="grid grid-cols-1 @md:grid-cols-2">
<div>Card</div>
<div>Card</div>
</div>
</div>The inner grid has one column by default. When the @container element is at least @md wide, the grid switches to two columns. The viewport width does not matter here.
The model is still mobile-first. An unprefixed utility applies at every container width, and @md: applies from that container width upward.
What `@container` does in CSS
The @container class sets container-type: inline-size on the element. This tells the browser to track the element's inline size (its width, in horizontal writing modes) so descendants can query it.
Inline-size containment has a cost. The browser must compute the container's width without looking at its content. That gives three consequences.
- A container cannot use its own width to restyle itself. Only descendants can respond. If you put
@md:p-8on the@containerelement, it will not query itself. - A container whose width normally comes from its content collapses. This affects
inline-block,inline-flex,float, and flex or grid items that shrink to fit. Give the container a width from its parent, such as a block-level element,w-full, or a grid or flex track. - Only the width is tracked. Tailwind's
@containerdoes not track height.
The practical rule is to use a wrapper element as the container and style the children inside it.
<div class="@container">
<article class="flex flex-col gap-3 @sm:flex-row">
<div class="h-24 w-full bg-gray-200 @sm:w-32"></div>
<div>Content</div>
</article>
</div>The <article> is a descendant, so it can respond to the wrapper's width.
The container size scale
Container variants use their own scale. The sizes are in rem, so they scale with the user's font size, and they are smaller than the viewport breakpoints.
| Variant | Minimum container width | Pixels at 16px root |
|---|---|---|
| `@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 |
Do not assume @md: equals md:. The viewport md is 48rem, while the container @md is 28rem. A common mistake is to copy a viewport design and expect identical switch points.
A worked example: one card, three places
This page puts the same card markup in a main column and in a narrow sidebar. You can save it as an HTML file and open it in a browser. It loads Tailwind v4 from the browser build, which is for experiments, not production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Container query demo</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="bg-gray-50 p-6">
<div class="mx-auto grid max-w-5xl grid-cols-1 gap-6 lg:grid-cols-[1fr_18rem]">
<main class="@container">
<article class="flex flex-col gap-4 rounded border bg-white p-4 @lg:flex-row">
<div class="h-32 rounded bg-blue-200 @lg:h-auto @lg:w-48"></div>
<div>
<h2 class="text-lg font-bold @lg:text-2xl">Wide placement</h2>
<p class="text-gray-600">Image sits beside the text once the main column is 32rem wide.</p>
</div>
</article>
</main>
<aside class="@container">
<article class="flex flex-col gap-4 rounded border bg-white p-4 @lg:flex-row">
<div class="h-32 rounded bg-blue-200 @lg:h-auto @lg:w-48"></div>
<div>
<h2 class="text-lg font-bold @lg:text-2xl">Narrow placement</h2>
<p class="text-gray-600">The sidebar is 18rem wide, so this card stays stacked.</p>
</div>
</article>
</aside>
</div>
</body>
</html>The two <article> elements have identical classes. The main column is wider than 32rem on most desktop windows, so its card shows the row layout. The sidebar is 18rem, so its card stays stacked. A viewport variant like lg:flex-row could not produce this difference.
On a phone, the page grid collapses to one column. The sidebar then becomes full width and its card switches to the row layout. That is the intended behavior, because the card now has room.
Choosing between viewport and container variants
The decision depends on what determines the available space.
Use these guidelines.
- Page shell, top navigation, and the number of main columns: use viewport breakpoints. These depend on the window.
- Cards, widgets, form groups, and anything placed in sidebars, modals, or grids of varying size: use container queries.
- You can mix both. A page can use
lg:grid-cols-[1fr_18rem]for the shell, and the cards inside use@lg:variants.
Max variants and ranges
Container queries have max forms, written @max-*. They apply below a container width.
<div class="@container">
<p class="@max-md:text-sm">Smaller text when the container is under 28rem</p>
<p class="@sm:@max-lg:underline">Underlined from 24rem up to, but not including, 32rem</p>
</div>Stacking @sm: with @max-lg: produces a range, like md:max-lg: in viewport breakpoints. The upper bound is exclusive.
Mobile-first still applies. Prefer @md: over @max-sm: for the main flow, and keep @max-* for exceptions.
Named containers
Nested containers are common. By default a container variant queries the nearest ancestor marked with @container. To target a specific ancestor, give containers names with a slash and use the same name in the variant.
<div class="@container/main">
<div class="@container/card">
<div class="flex flex-col @md/main:flex-row @sm/card:gap-6">
Responds to two different containers
</div>
</div>
</div>The @md/main: variant reads the outer container. The @sm/card: variant reads the inner one. Without names, both would read the inner container.
Arbitrary and custom container sizes
For a single threshold, use square brackets. The @min-[...] and @max-[...] forms accept any length.
<div class="@container">
<div class="@min-[475px]:flex @max-[360px]:hidden">One-off container sizes</div>
</div>For a size you want to reuse, add a container theme variable in v4. Variables starting with --container- define the names after the @.
@import "tailwindcss";
@theme {
--container-8xl: 96rem;
}After this, @8xl: is available. This variable also feeds the max-w-8xl utility, so adding it affects width utilities too. Keep that in mind before choosing names.
Container query units
Inside a container, the cqw unit is 1% of the container's width. Tailwind has no special utilities for it, but arbitrary values work.
<div class="@container">
<h2 class="text-[clamp(1rem,5cqw,2rem)]">Heading that scales with its container</h2>
</div>If no ancestor is a container, cqw falls back to the small viewport unit, so check that a container exists when the result looks wrong.
Tailwind v3
Tailwind v3 does not include container queries by default. Install the official @tailwindcss/container-queries plugin and add it to the plugins list in tailwind.config.js. The same @container, @md: and @container/name syntax then works. The plugin's scale differs slightly from v4 in some sizes, so check the plugin's documentation before copying a design between versions.
Common mistakes
- Putting
@md:on the container itself. The container cannot query its own width. Move the classes to a child. - Forgetting
@container. Without a marked ancestor,@md:finds no container and the style never applies. The rule is silent, with no error. - Letting the container shrink to its content. An
inline-flexor floated container collapses. Make it a block with a width from its parent. - Assuming
@mdequals the viewportmd. The container scale starts much smaller. - Wrapping every element in
@container. It adds containment, which affects layout and sizing. Mark only the elements whose width components need to read. - Expecting height queries. Tailwind's container variants query inline size only.
Debugging container queries
- Open the browser's developer tools and select the wrapper. Look for
container-type: inline-sizein the computed styles. Chromium and Firefox label container elements in the Elements panel. - Check the wrapper's rendered width. If it is 0 or very small, apply the shrink-to-fit fix above.
- Compare the width with the table in this chapter. Remember that
remvalues scale with the root font size. - With nested containers, look for a closer container that is capturing the variant. Use names if needed.
- Resize the container, not only the window. Drag a sidebar, or change a parent's
w-*class in the inspector.
Exercise
A card in a sidebar still shows the row layout (@md:flex-row) even though the sidebar is only 15rem wide. The card's classes are @container flex flex-col @md:flex-row. What is wrong, and how do you fix it?
Show answer
The @container class is on the same element as the @md: variant. A container cannot query its own width, so @md: does not use the sidebar's width as intended. The variant also does not read the 15rem sidebar, because no ancestor is marked as a container. Wrap the card in a parent with @container, and keep flex flex-col @md:flex-row on the child. At 15rem the sidebar is below @md (28rem), so the child stays stacked.
Exercise
You want a heading to be text-sm below 24rem container width, text-base from 24rem up to 42rem, and text-xl from 42rem. Write the classes using mobile-first container variants, and name the container panel.
Show answer
Use text-sm @sm/panel:text-base @2xl/panel:text-xl on the heading, inside a parent with @container/panel. The unprefixed text-sm applies below 24rem. @sm/panel: starts at 24rem and @2xl/panel: starts at 42rem and overrides it.