Sub topic 7
Nested and named containers
This chapter assumes you can already mark an element with @container and style children with variants such as @md:. It covers what happens when containers sit inside other containers, and how to choose which one a variant reads.
The default rule: the nearest container wins
A container variant such as @md:flex-row is a container query. The browser must decide which ancestor to measure. Without a name, it picks the nearest ancestor that is a query container.
Take this markup:
<div class="@container">
<div class="@container">
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>
</div>The @md:flex-row class reads the inner container. The outer one is ignored, even if it is the one that is wide enough. This is the same lookup that CSS custom properties and inherited styles use: the closest matching ancestor, walking upward.
That rule is enough for simple components. It breaks down when a component needs to react to a container that is not its closest one. The usual case is a page layout with a main area and a sidebar, where a child must respond to the main area's width even though it sits inside another container.
Why nesting happens
Nested containers are common in real interfaces:
- A dashboard grid cell is a container, and the card inside it is also a container.
- A reusable card marks itself as a container, then is placed inside a page region that is also a container.
- A design system wraps every panel in
@container, and panels contain other panels.
Each of these produces a chain of containers. Without names, every variant binds to the closest link in that chain. That behavior is fine until someone adds a wrapper in the middle, which silently changes which container the variants read.
Naming a container
Add a name after a slash on the @container class:
<div class="@container/main"></div>Tailwind v4 generates both container-type: inline-size and container-name: main for this class. The name is a label that a query can refer to.
To query a named container, add the same name after a slash on the variant:
<div class="flex flex-col @md/main:flex-row"></div>This produces a query that targets only the ancestor named main. Its size threshold is the usual @md size, 28rem. Ancestors with other names, and unnamed ancestors, are skipped.
The slash syntax matches other named features in Tailwind, such as group/item and peer/name. If you know those, the idea is the same: the name after the slash links the marker to the thing that reads it.
A worked layout with two named containers
Here is a page with a main column and a sidebar. Each region is its own container, and a single element reads from both.
<div class="flex gap-6">
<main class="@container/main min-w-0 flex-1">
<section class="grid grid-cols-1 gap-4 @lg/main:grid-cols-2">
<article class="rounded bg-white p-4 shadow">Post one</article>
<article class="rounded bg-white p-4 shadow">Post two</article>
</section>
</main>
<aside class="@container/sidebar w-72 shrink-0">
<ul class="flex flex-col gap-2 @xs/sidebar:gap-4">
<li>Recent</li>
<li>Popular</li>
<li>Archive</li>
</ul>
</aside>
</div>Here the two containers are siblings, not nested, so each variant has only one candidate. Names are not required here. Using them still documents which region each class depends on, and it keeps working if someone later wraps the grid in another container.
Now the nested case, adapted from this chapter's reference example:
<div class="@container/main">
<aside class="@container/sidebar">
<div class="flex flex-col @md/main:flex-row @lg/sidebar:gap-8">
<div>A</div>
<div>B</div>
</div>
</aside>
</div>The div has two container ancestors: sidebar (the nearest) and main (further out). It uses both:
@md/main:flex-rowswitches to a row when the outermaincontainer is at least 28rem wide.@lg/sidebar:gap-8widens the gap when the innersidebarcontainer is at least 32rem wide.
The sidebar is closer, but the /main suffix makes the browser skip it and keep walking up. A single element can mix names freely, and each variant resolves independently.
What an unnamed variant does with named containers
A named container is still a container. An unnamed variant such as @md: does not ignore it. It matches the nearest ancestor that is a container of any name.
<div class="@container/main">
<div class="flex flex-col @md:flex-row">...</div>
</div>Here @md: reads main, because main is the nearest container. Adding a name to the container does not stop unnamed variants from using it. This lets you adopt names gradually.
Same name, repeated
Names do not have to be unique. If two ancestors share a name, a named variant reads the nearest one with that name. This is useful for a recursive component, such as a comment thread or a nested panel, where each level is @container/panel:
<div class="@container/panel">
<div class="flex flex-col @sm/panel:flex-row">
<div>Label</div>
<div class="@container/panel">
<div class="flex flex-col @sm/panel:flex-row">Nested content</div>
</div>
</div>
</div>Each @sm/panel:flex-row reads the closest panel above it. The nested content responds to its own panel's width, not the outer one.
Choosing between named and unnamed
| Situation | Recommended approach |
|---|---|
| A component and its one container, with nothing between them | Unnamed. It is shorter and works. |
| A reusable component that sets its own `@container` | Unnamed for its internal layout, so it adapts to its own box. |
| A child that must follow a distant ancestor | Named, so the nearest-container rule cannot intercept. |
| A layout with several regions that are all containers | Named, so each region's classes say what they depend on. |
| Code that other teams will wrap in arbitrary markup | Named, so added wrappers cannot change which container is read. |
A practical guide is to leave names off until you have two containers in one ancestor chain, then add them to the ones that are ambiguous.
Containers do not style themselves
A container variant applies to descendants of the container, never to the container itself. This breaks many first attempts:
<div class="@container @md:p-8">Does not work</div>The @md:p-8 class asks about an ancestor of this element. Because the element is not inside itself, the query looks further up, and there may be no container there. Put the padding on a child, or use a wrapper:
<div class="@container">
<div class="p-2 @md:p-8">Works</div>
</div>The same applies when one container is nested inside another. The inner container can read the outer one, because the outer one is its ancestor. The inner container cannot read itself. If the inner container needs a style tied to its own width, put that style on a child of the inner container.
The rule exists because container queries measure the container's size, and a container's own size can depend on its styles. If an element could change its own width based on a query about its own width, the result could loop. The browser prevents this by only letting descendants respond.
Container sizing gotchas
@container sets container-type: inline-size. This applies inline-size containment: the element's width can no longer depend on its content. That is what makes the query stable, and it changes how some layouts behave.
- A block element fills its parent's width, so it works as a container with no extra effort.
- A flex item with
flex: 0 1 autoand no explicit width can collapse to zero width, because the browser no longer sizes it from its content. Give it a width,flex-1, orw-full. - A grid column sized with
autohas the same problem. Use1fr, a fixed size, orminmax(). - An element with
inline-blockorinline-flexdisplay can shrink to nothing for the same reason.
In the two-region layout above, the main element has min-w-0 flex-1. The flex-1 gives it a width, and min-w-0 lets it shrink below its content size so that the container width reflects the real space.
Debugging nested containers
When a variant does nothing, work through these checks in order:
- Does the element have an ancestor with
@container? A missing marker is the most common cause. - If the variant has a name, does an ancestor have exactly the same name?
@md/mainneeds@container/main. A typo such as@container/mianfails without an error. - Is the variant on the container itself instead of a descendant?
- Is the nearest container the one you meant? Insert names to find out.
- Has the container collapsed in width? Check its computed size.
- In Tailwind v3, is the
@tailwindcss/container-queriesplugin installed? Without it, none of the variants exist.
A quick technique is to add a temporary visible style to the container's direct child, such as bg-red-200 @md/main:bg-green-200. The color tells you at a glance whether the query is matching and which container it reads.
Summary of the rules
- An unnamed variant reads the nearest ancestor that is a container, whatever its name.
- A named variant such as
@md/main:reads the nearest ancestor with that exact name and skips others. @container/namecreates the named container, and@size/name:queries it.- A container cannot be queried by its own classes. Only descendants respond.
- Reusing a name is allowed, and the nearest one wins.
- Adding a wrapper container between a child and its intended container changes what unnamed variants read. Names prevent this.
Exercise
A page has <div class="@container/page"> containing <section class="@container">, which contains <div class="grid grid-cols-1 @lg:grid-cols-2 @md/page:gap-8">. The page container is 40rem wide and the section is 20rem wide. Which of the two variants apply to the grid, and why?
Show answer
@lg:grid-cols-2 does not apply. The unnamed variant reads the nearest container, which is the section at 20rem, and @lg needs at least 32rem. @md/page:gap-8 applies. The /page suffix makes it skip the section and read the page container, which is 40rem wide and above the 28rem @md threshold.
Exercise
You write <div class="@container @md:p-8"> and the padding never changes, even in a wide container. Give two ways to fix it.
Show answer
A container variant only applies to descendants, so the element cannot respond to its own width. Either move @md:p-8 onto a child element inside the container, or place the element inside a separate ancestor that has @container, so the query measures that ancestor.