Sub topic 12
Common pitfalls and debugging
This chapter assumes you can write Tailwind utility classes, you know the viewport variants (md:, lg:), and you have used @container with variants such as @md: as shown earlier in this tutorial. Container queries fail silently. A variant that doesn't match produces no error, so you debug by checking a short list of causes in a fixed order.
How a container variant gets resolved
A class like @md:flex-row compiles to a CSS rule wrapped in a container query. In Tailwind v4 the output looks like this:
@container (width >= 28rem) {
.\@md\:flex-row {
flex-direction: row;
}
}The browser applies that rule only when three conditions hold:
- The element has an ancestor that is a query container (
container-type: inline-size, which the@containerclass sets). - That ancestor is the one the query resolves to. This is the nearest query container, or the nearest one with a matching name.
- The ancestor's content-box width is at least
28rem.
Almost every pitfall below breaks one of these three conditions.
Pitfall 1: no `@container` ancestor
This is the most common mistake. The variants are present, but nothing marks a container.
<!-- Broken: no ancestor is a query container -->
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>With no query container above it, the @container rule can never match, so the layout stays a column at every width. The fix is to mark an ancestor.
<div class="@container">
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>A component that uses @md: classes depends on its parent providing a container. Many teams document this with a comment, or wrap the component in its own @container element, so the dependency is not hidden.
Pitfall 2: the variant is on the container itself
A container query always looks at an ancestor. An element can't query its own size.
<!-- Broken: the element is the container and also uses @md: -->
<div class="@container flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>The @md:flex-row here looks for a container above this div. If none exists, it does nothing. If a distant ancestor is a container, the element responds to that ancestor's width, which is a confusing bug. The fix is to separate the two roles. The outer element marks the container, and an inner element carries the variants.
<div class="@container">
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>The extra wrapper costs one element. That is the price of the browser's rule that a container cannot style itself based on its own size.
Pitfall 3: the container collapses to zero width
@container sets container-type: inline-size. This applies inline-size containment, which means the element's width cannot be computed from its contents. The browser treats the content's contribution to the container's width as zero.
In a normal block layout this is harmless, because a block fills its parent. It becomes a problem when the container sits in a shrink-to-fit context, where the width normally comes from the content:
- A flex item with no
flex-1,grow, or explicit width. - An
inline-blockorinline-flexelement. - A floated element.
- A grid column sized with
auto.
<!-- Broken: the container is a flex item that sizes to its content, which counts as zero -->
<div class="flex">
<div class="@container">
<div class="flex flex-col @md:flex-row">
<div>A card with plenty of text inside</div>
</div>
</div>
</div>The container shrinks to nearly nothing, so @md: never matches and the text may wrap one word per line. Give the container a width source.
<div class="flex">
<div class="@container flex-1">
<div class="flex flex-col @md:flex-row">
<div>A card with plenty of text inside</div>
</div>
</div>
</div>w-full, grow, w-64, or a grid track such as 1fr work too. If the layout looks wrong in a way that suggests the content is missing or squashed, check the container's rendered width in the browser first.
Pitfall 4: padding and the content box
Container queries measure the container's content box, which excludes padding and borders. With box-sizing: border-box, which Tailwind's preflight sets, a container with w-[30rem] p-8 has an outer width of 30rem. The content box is 30rem minus 4rem of horizontal padding, so 26rem.
The variant @md needs 28rem. It will not apply, even though the element looks 30rem wide in the inspector.
<!-- The query sees 26rem, not 30rem, so @md does not match -->
<div class="@container w-[30rem] p-8">
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>You can move the padding to the inner element, or choose a smaller variant. Moving the padding inward keeps the container's content box equal to its outer width.
<div class="@container w-[30rem]">
<div class="p-8">
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>
</div>The same content-box rule applies to container query units such as cqw, so w-[50cqw] is half of the content width, not half of the outer width.
Pitfall 5: mixing up viewport and container variants
md: and @md: look similar and mean different things. They also use different sizes.
| Variant | Measures | `md` threshold |
|---|---|---|
| `md:` | Viewport width | 48rem (768px) in default Tailwind v4 |
| `@md:` | Nearest container width | 28rem (448px) |
Two consequences follow. First, copying a class from md:flex-row to @md:flex-row changes both what is measured and where the switch happens. Second, a 28rem container can sit inside a 1400px viewport, so @md: is active while lg: and xl: are also active. Both sets of variants can apply to one element, and one can undo the other. If you mix them on one element, decide which one owns each property.
Pitfall 6: nested and named containers
An unnamed variant such as @md: resolves to the nearest ancestor that is a query container, whether or not that ancestor has a name. An inner @container shadows an outer one.
<div class="@container">
<div class="@container">
<!-- @md: measures the inner container, not the outer one -->
<div class="flex flex-col @md:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>
</div>Naming fixes this. The name goes after a slash, on both the container and the variant.
<div class="@container/main">
<div class="@container/sidebar">
<div class="flex flex-col @md/main:flex-row">
<div>A</div>
<div>B</div>
</div>
</div>
</div>Names must match exactly. These mistakes all produce no output and no warning:
- Writing
@md/mian:when the container is@container/main. - Writing
@md/main:when no ancestor has@container/main. - Using a name on the variant but forgetting
@containeron the ancestor, and only writing/mainsomewhere else.
When a named variant does nothing, search the file for the name and confirm that it appears on an ancestor of the element.
Pitfall 7: class names Tailwind cannot see
Tailwind generates CSS by scanning your source files for complete class names. A class assembled at runtime is never found, so no rule is generated.
<!-- Broken in a template where size is a variable: Tailwind cannot see @md:flex-row -->
<div class="@${size}:flex-row"></div>Write the full class name somewhere in a scanned file, for example in a lookup table.
const layoutBySize = {
sm: "@sm:flex-row",
md: "@md:flex-row",
lg: "@lg:flex-row",
};
export function layoutClass(size) {
return layoutBySize[size] ?? "";
}The same applies to custom sizes. If you define --container-8xl in @theme, use the exact name @8xl: in your markup. A typo such as @8x1: generates nothing.
Pitfall 8: Tailwind version mismatch
Container queries are built into Tailwind v4, so no plugin is needed. In Tailwind v3 they come from the @tailwindcss/container-queries plugin. Two failures follow from this.
- On v3 without the plugin,
@containerand@md:are not recognized, and nothing is generated. - On v4, the v3 plugin and its config entry are unnecessary. Remove them when you migrate.
Check which version you run before you change any markup.
npm ls tailwindcssIf the output shows version 3.x, confirm that plugins: [require('@tailwindcss/container-queries')] is in tailwind.config.js. Also check that the dev server was restarted after you added it.
Pitfall 9: expecting height queries
The @container class applies container-type: inline-size. That tracks width only. Variants such as @md: therefore cannot react to a container's height. Size containment on both axes needs container-type: size, which requires the container to have a definite height from some other source, because its content can no longer give it one. For most component work, width queries are what you want. If you think you need height queries, first check whether a plain layout (min-h-*, flex, or grid) solves the problem.
A debugging routine
Work through these steps in order. Each step rules out one condition from the start of the chapter.
- Check the generated CSS. In the browser's DevTools, look at the element's Styles panel. The rule for
@md:flex-rowshould appear under an@container (width >= 28rem)block. If it is missing, the class was not generated. Look for dynamic class names, typos, or a missing v3 plugin. - Check for a container. Walk up the DOM from the element. Some ancestor should have
container-type: inline-sizein its computed styles. Chromium-based DevTools also mark container elements with a badge in the Elements panel. - Check the element is not its own container. Make sure the variant is on a descendant of the container.
- Check which container resolves. Look for an inner
@containerthat shadows the one you meant, and check that names match. - Measure the container. Read its content-box width in DevTools, not its outer width. Compare it to the size scale:
@smis 24rem,@mdis 28rem,@lgis 32rem,@xlis 36rem, and@2xlis 42rem. - Probe with a loud class. Add a temporary
@min-[1px]:bg-red-500to the element. If the background does not turn red, the container is missing or the class is not generated. If it does, the container works and the problem is the breakpoint or a conflicting utility.
Summary table
| Symptom | Likely cause | Fix |
|---|---|---|
| Variant never applies | No `@container` ancestor | Add `@container` to a parent |
| Variant never applies, container exists | Variant is on the container itself | Move it to a child |
| Container is tiny or text wraps by word | Inline-size containment in a shrink-to-fit context | Add `w-full`, `flex-1`, or a width |
| Breakpoint triggers later than expected | Padding reduces the content box | Move padding to an inner element |
| Wrong ancestor responds | Nested containers | Name containers and variants |
| Named variant does nothing | Name mismatch | Match names exactly |
| Class missing from CSS | Runtime-built class name or typo | Write the full class name in source |
| `@container` unknown | Tailwind v3 without plugin | Install and register the plugin |
Exercise
A card has class="@container flex flex-col @md:flex-row" on a single div, and it never switches to a row at any width. Give two reasons it fails, and rewrite the markup so that it works.
Show answer
The @md:flex-row variant queries an ancestor container, not the element that carries it, so the element cannot respond to its own width. Any ancestor container is far away or missing, so the query never matches. Separate the roles: <div class="@container"><div class="flex flex-col @md:flex-row">...</div></div>. If the outer div sits in a flex row, also give it flex-1 or w-full so it does not collapse.
Exercise
A container is w-[30rem] p-8 and a child uses @md:flex-row (28rem). The child stays in a column. Why, and what are two ways to fix it?
Show answer
Container queries measure the content box. With 2rem of padding on each side, the content width is 26rem, which is below the 28rem threshold. Either move the padding to an inner element so the container's content box stays 30rem, or use a smaller variant such as @sm: (24rem) so 26rem still qualifies.
Exercise
You wrote @md/sidebar:hidden on an element, and it has no effect. List three things to check.
Show answer
First, confirm an ancestor of the element has @container/sidebar, with the exact same spelling. Second, confirm the class appears as a full string in a scanned source file, not built at runtime. Third, check that the sidebar container's content-box width reaches 28rem and that the container is not collapsed to zero width.