Sub topic 11
Component-level responsiveness with container queries
This chapter assumes you know flexbox and grid, the Tailwind breakpoint prefixes (sm:, md:, lg:), and what a CSS media query is. It covers container queries, which respond to the width of a parent element instead of the viewport. The examples use Tailwind v4.
When the viewport is the wrong measure
Breakpoint prefixes such as md: compile to media queries. They compare the viewport width to a threshold. That works for page-level layout, but it fails for reusable components.
Suppose a card is placed in a wide main column on one page and in a narrow sidebar on another. On a 1200px viewport, md:grid-cols-2 fires in both places. The sidebar copy of the card gets squeezed into two cramped columns, because the card has no way to know how much room it actually has.
A container query fixes this. The component asks "how wide is my parent?" and not "how wide is the window?"
Marking a container and querying it
Two steps are required:
- Add
@containerto the parent. This makes it a query container. - Use
@-prefixed variants such as@md:on its descendants.
<div class="@container">
<div class="grid grid-cols-1 @md:grid-cols-2">
<div>Card</div>
<div>Card</div>
</div>
</div>The grid shows one column by default. When the @container parent is at least the @md width, it switches to two columns. The viewport size does not matter.
The mobile-first rule still applies. An unprefixed utility applies at every container width. @md: means "at this container width and wider".
Container sizes
Container variants use their own scale, based on rem values. These are the common ones in v4.
| Variant | Minimum container width |
|---|---|
| `@sm:` | 24rem (384px) |
| `@md:` | 28rem (448px) |
| `@lg:` | 32rem (512px) |
| `@xl:` | 36rem (576px) |
| `@2xl:` | 42rem (672px) |
These values are smaller than the viewport breakpoints because components are usually smaller than the page. Do not assume @md: equals md:.
Reusing one component in two places
The next example places the same card markup in a wide area and a narrow one. Each copy chooses its own layout.
<div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
<main class="@container lg:col-span-2">
<article class="flex flex-col gap-3 rounded border p-4 @md:flex-row">
<div class="h-24 w-full rounded bg-gray-200 @md:w-32">Image</div>
<p>Wide column: the image sits beside the text.</p>
</article>
</main>
<aside class="@container">
<article class="flex flex-col gap-3 rounded border p-4 @md:flex-row">
<div class="h-24 w-full rounded bg-gray-200 @md:w-32">Image</div>
<p>Narrow column: the image stays above the text.</p>
</article>
</aside>
</div>The page grid uses a viewport breakpoint (lg:) to place the two columns. The cards use container variants (@md:) to arrange their contents. The identical card stacks in the sidebar and goes horizontal in the main column.
Choosing between the two
- Use viewport breakpoints for page structure: headers, navigation, and the overall column layout.
- Use container queries for components that can be placed in containers of different widths, such as cards, widgets, and form groups.
- A component can use both. The page decides how wide the container is, and the component adapts to that width.
Common errors
- Forgetting
@container. Without it,@md:classes do nothing, because no ancestor is a query container. This is the most common cause of "my container query doesn't work". - Putting
@containerand the@md:utility on the same element. A container cannot query itself. Apply the variant to a descendant. - Assuming a container's width follows from content. A query container needs a width set by its parent or its own styles. Marking an element as a container stops its inline size from depending on its children.
- Mixing up the two scales.
md:is 768px of viewport.@md:is 448px of container.
Exercise
A card uses grid-cols-1 @lg:grid-cols-2, but it always shows one column, even inside a very wide parent. Name two likely causes.
Show answer
First, no ancestor has the @container class, so the @lg: variant has nothing to query. Second, @container is on the same element as @lg:grid-cols-2, and an element cannot query itself. The fix is to put @container on a parent of the grid. A third possibility is a parent narrower than 32rem (512px), the @lg threshold, though the question says the parent is very wide.
Exercise
A page has a sidebar and a main column. Which kind of breakpoint should place the two columns side by side, and which should rearrange the contents of a card inside each column?
Show answer
Use a viewport breakpoint such as lg: to place the columns, because that is page structure. Use container variants such as @md: inside the card, because the card's available width depends on its column and not on the viewport.