Sub topic 12
Common pitfalls and debugging
This chapter assumes you know flexbox and grid, Tailwind utility classes, and responsive variants such as md:. It also assumes you have read the earlier chapters on @container, container variants, named containers, and the size scale. Here you will learn the errors that show up most often and a short routine for finding them.
How a container query fails
A container variant such as @md:flex-row compiles to a CSS rule that is wrapped in a container query. That query needs an ancestor with container-type set. If none exists, the query never matches. The browser does not warn you and the class does nothing.
Almost every problem in this chapter comes down to one question: which element is the browser measuring, and is it the one you expect?
Pitfall 1: the missing @container ancestor
This is the most common error. The variant is on the element, but no parent is marked.
<!-- Broken: no ancestor has @container -->
<div class="flex flex-col @md:flex-row">
<div>Left</div>
<div>Right</div>
</div>The fix is to add @container to a parent:
<div class="@container">
<div class="flex flex-col @md:flex-row">
<div>Left</div>
<div>Right</div>
</div>
</div>Pitfall 2: styling the container itself
A container cannot respond to its own size. Variants only affect descendants. If you put @container and @md:flex-row on the same element, the query looks for a container above it, not at it.
<!-- Broken: the element is its own container -->
<div class="@container flex flex-col @md:flex-row">...</div>
<!-- Fixed: add a wrapper -->
<div class="@container">
<div class="flex flex-col @md:flex-row">...</div>
</div>The extra wrapper costs one element. It is the standard pattern.
Pitfall 3: the nearest container is not the one you meant
Without a name, a variant uses the nearest ancestor container. In nested layouts that is often a small inner box rather than the page-level region you had in mind.
<div class="@container/main">
<aside class="@container/sidebar">
<div class="flex flex-col @md/main:flex-row">
<div>A</div>
<div>B</div>
</div>
</aside>
</div>Here @md/main targets the outer container. Without /main, the sidebar would be measured instead. If a name on the variant does not match any ancestor name, the query never matches. Check the spelling on both sides.
Pitfall 4: a container with no usable width
Marking an element as a container applies size containment on the inline axis. The element can no longer take its width from its children. Some layouts therefore collapse:
- A container inside a shrink-to-fit parent, such as an inline-block, a float, or a flex item with no width, can shrink to zero width.
- A container in a flex row with no
flex-1,w-*, orgrowmay be measured at a size you did not expect.
When a variant seems to fire at the wrong time, open dev tools and check the container's computed width. Give it an explicit width, or let it fill its parent with w-full or flex-1.
Pitfall 5: mixing up viewport and container variants
md: and @md: look alike and mean different things. md: compares the viewport to 48rem. @md: compares the container to 28rem. The container scale is smaller, so values do not line up with the viewport scale.
| Variant | Measures | Default threshold |
|---|---|---|
| `md:` | Viewport width | 48rem |
| `@md:` | Container width | 28rem |
| `@max-md:` | Container width, below the size | 28rem |
Pitfall 6: version and build problems
In Tailwind v4, container queries are in core. In v3, they need the official plugin. If @md: classes produce no CSS in a v3 project, check that the plugin is installed and listed:
// tailwind.config.js (Tailwind v3 only)
module.exports = {
theme: {
extend: {},
},
plugins: [require('@tailwindcss/container-queries')],
}A custom size such as @8xl also needs its theme variable in v4:
@import "tailwindcss";
@theme {
--container-8xl: 96rem;
}Tailwind finds classes by scanning your source files, so write class names in full. A name built from string pieces, such as "@" + size + ":flex-row", is never generated.
A debugging routine
- Inspect the element and confirm that the
@md:rule appears inside an@containerblock in the Styles panel. - Walk up the DOM and find the ancestor that has
container-type: inline-size. - Read that ancestor's width and compare it with the threshold. For
@mdthat is 28rem, which is 448px at the default font size. - If the container has a name, confirm the variant uses the same name.
- Make sure the container is not the same element you are styling.
Exercise
A card has the classes @container flex flex-col @lg:flex-row on a single div. It never switches to a row, however wide the page is. Why, and how do you fix it?
Show answer
The element is its own container, and container variants only respond to an ancestor container. Wrap it in a parent that has @container and keep flex flex-col @lg:flex-row on the inner element.
Exercise
@md/main:flex-row has no effect, and the nearest ancestor has the class @container/sidebar. The page has no @container/main ancestor. What is wrong?
Show answer
The variant names main, but no ancestor has that name, so the query never matches. Add @container/main to the intended ancestor, or change the variant to @md/sidebar if the sidebar is the container you want to measure.