Sub topic 2
Marking a Container with @container
This chapter covers how to turn an element into a query container. I assume you know flexbox and grid, Tailwind utility classes, and responsive variants such as md: and lg:. The earlier chapter explained why container queries exist. Here you learn the one class that makes them work.
What @container does
A container query asks "how wide is my parent?" instead of "how wide is the screen?". The browser can only answer that for an element you have explicitly marked. In Tailwind, you mark it with the @container class.
Under the hood, @container sets the CSS property container-type: inline-size on the element. This tells the browser to track the element's inline size, which is its width in horizontal writing modes. Descendants can then use container variants such as @md: and be styled according to that width.
<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 container. The inner div stacks its children in a column by default. Once the container is at least @md wide, the layout switches to a row. The viewport width does not matter.
The rule: the container is an ancestor, never the element itself
A container variant styles descendants of the container. It never styles the container itself. This is the most common source of confusion.
<!-- Wrong: the @md: variant has no ancestor container to query -->
<div class="@container @md:bg-blue-500">...</div>
<!-- Right: the child responds to its parent -->
<div class="@container">
<div class="@md:bg-blue-500">...</div>
</div>The browser forbids an element from querying its own size, because its size could depend on its own styles and create a loop. That is why you usually need a wrapper element whose only job is to be the container.
How a variant finds its container
When a child uses @md:, the browser looks up the tree for the nearest ancestor that is a query container. It uses that ancestor's width for the comparison. If no ancestor is marked, the variant never matches and the base style stays in effect. The browser does not fall back to the viewport.
The diagram shows the lookup.
Choosing where to place the container
Place @container on the element whose width should drive the layout. Two placements are common.
- On a wrapper inside the component. The component then adapts to whatever space its parent gives it. This is the usual choice for reusable components.
- On a layout region. For example, mark a sidebar or main column so that everything inside responds to that region.
Because @container is just a class, a container can also be a flex or grid item. Its width is the width the layout gives it, and the query follows that width.
Side effects to know about
Setting container-type: inline-size applies size containment on the inline axis. In practice, this means:
- The container's width cannot depend on its contents. A container inside a shrink-to-fit parent, such as an
inline-blockor a flex item with no width, can collapse to zero width. Give it a width withw-full,flex-1, a grid column, or a block-level parent. - Its height is still determined by its contents, so vertical layout works as usual.
<!-- Safe: block-level container fills its parent -->
<div class="@container">
<div class="@lg:flex">...</div>
</div>
<!-- Risky: inline-block shrinks to fit, so its width is unreliable -->
<div class="@container inline-block">
<div class="@lg:flex">...</div>
</div>Marking a container in Tailwind v3
Tailwind v4 supports @container in core. In v3 you need the official plugin, which a later chapter covers. Without it, @container and the @md: variants generate no CSS.
Exercise
A developer writes <div class="@container @lg:grid-cols-3 grid">...</div> and the grid never changes to three columns, even in a very wide container. Why, and how do you fix it?
Show answer
The @lg: variant is on the container element itself. Container variants only style descendants of a container, and an element cannot query its own size. Wrap the grid in a parent with @container, so that the markup is <div class="@container"><div class="grid @lg:grid-cols-3">...</div></div>.
Exercise
What CSS property does the @container class set, and what happens to a child's @md: variant if no ancestor has that class?
Show answer
It sets container-type: inline-size. If no ancestor is a query container, the @md: variant never matches, so the child keeps its base styles. It does not fall back to the viewport width.