Sub topic 7
Nested and named containers
This chapter assumes you can mark an element with @container, style children with variants such as @md:, and read the default container size scale. Here you will learn what happens when containers sit inside other containers, and how to pick the one you want.
The nearest container wins
A container variant such as @md: always responds to the nearest ancestor marked with @container. It does not look further up the tree. In most layouts this is what you want, because a component should react to the space it was given.
Nesting causes trouble when the nearest container is not the one you meant. Suppose a page wrapper and a sidebar are both containers, and a child inside the sidebar should respond to the wrapper. An unnamed @md: will measure the sidebar and ignore the wrapper.
Naming a container
Add a name after a slash on the @container class. Then add the same name after the variant to target that specific ancestor.
<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>In this example:
@md/main:flex-rowapplies when themaincontainer is at leastmdwide, even thoughsidebarsits closer.@lg/sidebar:gap-8applies when thesidebarcontainer is at leastlgwide.- A variant with no name, such as
@lg:, would measure the nearest container, which here issidebar.
The name is only a label. It does not change how the container measures its size.
Naming and the nearest container together
Be careful with one detail: naming a container does not hide it from unnamed variants. An element with @container/sidebar is still a container. Unnamed variants below it will still respond to it.
| Variant on a child | Which container it measures |
|---|---|
| `@md:` | The nearest ancestor with any `@container` class |
| `@md/main:` | The nearest ancestor named `main` |
| `@md/sidebar:` | The nearest ancestor named `sidebar` |
If no ancestor has the requested name, the variant never applies. Tailwind does not fall back to another container, and it does not report an error.
Naming works with all variant forms
The name suffix works with the max and range forms too. This lets you build a range against one specific container.
<div class="@container/card">
<div class="flex flex-row @max-md/card:flex-col">
<div>Left</div>
<div>Right</div>
</div>
</div>The layout is a row by default and becomes a column when the card container is narrower than md.
When to name containers
Names add a small cost, because every reader must match the variant to the right ancestor. Use them when they solve a real problem.
- Skip names for a self-contained component, such as a card, that only ever needs its own container.
- Add names when a child must respond to a container that is not the nearest one.
- Add names when two containers are nested and you want each to drive different styles.
- Choose descriptive names such as
main,sidebar, orcard, not single letters.
Keep nesting shallow. If you need three levels of named containers to make a layout work, consider whether the structure can be simplified.
Common errors
- Name mismatch.
@container/sidebarpaired with@md/side:matches nothing. The text after the slash must be identical. - Name on the variant only.
@md/main:needs an ancestor with@container/main. A plain@containerdoes not match. - Container on the same element. A container cannot be queried by its own styles. The variant must be on a descendant, as in the examples above.
- Wrong container measured. If a style responds to the wrong width, check whether a closer unnamed container is intercepting the variant.
Exercise
A page has <div class="@container/page"> containing <section class="@container">, which contains a <p class="@lg:text-xl @lg/page:font-bold">. Which container does each variant measure?
Show answer
@lg:text-xl measures the nearest container, which is the section with the plain @container class. @lg/page:font-bold skips the section and measures the outer div named page.
Exercise
You wrote @md/sidebar:flex-row on a child, but nothing changes at any width. The sidebar is marked <aside class="@container">. What is wrong, and how do you fix it?
Show answer
The aside has no name, so no ancestor matches sidebar. Change the class to @container/sidebar, or remove the name from the variant if the aside is the nearest container.