Sub topic 13
Debugging and common pitfalls
This chapter assumes you have read the earlier chapters of this tutorial. You should know how container-type, container-name, and @container work, and you should be comfortable with flexbox, grid, and custom properties. Most container queries that "do nothing" fail for a small number of reasons. Here you learn to recognize each one and to check them in a fixed order.
The mental model for diagnosis
A container query rule applies to an element only when all of these are true:
- An ancestor of the element is a query container.
- That ancestor is the one the browser picks for this rule. See the chapter on which container a query matches.
- The container can answer the feature the rule asks about.
- The condition is true at the container's current size.
- The declaration wins the cascade against other rules.
Each pitfall below breaks one of these five points.
Pitfall 1: styling the container itself
A container query styles the descendants of the container. It never styles the container. If the element with container-type is also the element you want to change, nothing happens.
/* Broken: .card is the container and the target */
.card {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}The rule asks for a .card that sits inside a container, and no such element exists. The fix is a wrapper, which is the pattern used throughout this tutorial.
.card-wrapper {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}The wrapper is the container, and the card inside it is the target. If you cannot add markup, you can often use a child element of the card as the target instead.
Pitfall 2: no container-type
The default value of container-type is normal. An element with normal is not a size container. A size query with no eligible ancestor is false, so the rule never applies, and the browser reports no error. Check that the property is spelled correctly and is not overridden by a later rule. A typo such as container-type: inline is an invalid value, so the browser drops the declaration silently.
Style queries are the exception. An element can answer a style query without any container-type, because every element can be asked about its computed custom properties.
Pitfall 3: the container collapses
inline-size containment means the container's inline size cannot depend on its contents. This is what makes queries stable, because the children cannot change the width that the query measures. It also causes surprises when the container's width was previously determined by its children.
Containers that shrink to fit their content are the usual victims:
- A floated element.
- A flex item with
flex: noneor no basis. - A grid item in an
autotrack. - An
inline-blockor an absolutely positioned element with no width.
In each case the container's content no longer contributes to its width, so the width can drop to zero or to a very small value. Your query then evaluates against a tiny width, and the component looks stuck in its narrowest layout.
.layout {
display: flex;
gap: 1rem;
}
/* Give the container a width that does not depend on its children */
.card-wrapper {
container-type: inline-size;
flex: 1 1 20rem;
min-width: 0;
}flex: 1 1 20rem supplies a size that does not come from the contents. The min-width: 0 rule lets the item shrink below its content width when the row is tight.
The same logic applies to height with container-type: size. Size containment ignores the contents in both dimensions, so a container with no explicit height collapses to zero height. Prefer inline-size unless you need height queries, and give the container a definite height if you use size.
Pitfall 4: display values that cannot be containers
Size containment does not apply to every box. An element with display: inline cannot be a size container, and neither can display: contents, because the element generates no box of its own. A <span> used as a wrapper will not work. Set display: block (or inline-block with a width, or a flex or grid item) on the container. Also check that nothing sets display: contents on the wrapper, for example as a layout shortcut.
Pitfall 5: asking for a feature the container cannot answer
The value of container-type limits what you can query:
| container-type | Width and inline-size queries | Height and block-size queries |
|---|---|---|
| `normal` | No | No |
| `inline-size` | Yes | No |
| `size` | Yes | Yes |
This is why the earlier example @container (width > 700px) or (height > 500px) only works when the container uses container-type: size. With inline-size, the height half of the condition is never true. A query that mixes both with and can never match.
Pitfall 6: the rule matches a different container than you expect
An unnamed @container rule uses the nearest ancestor that is a query container. Nesting containers is normal, for example a page layout container that holds a card container. The rule then measures the nearest one, which may be much narrower than the one you had in mind.
A named container fixes this. Name the container and the rule together, as in container: sidebar / inline-size; and @container sidebar (width > 30rem). When a named rule does not apply, check these things:
- The name in the rule is spelled exactly like the name in
container-name. Names are case-sensitive. - The named container is an ancestor of the element, not a sibling or a descendant.
- A container can have several names, so the rule matches if any of them is the one used in the rule.
You can also unset a container when you no longer want it to answer queries by setting container-type: normal. Be careful with that in components that set the property on a variant class, because the unnamed rules in descendants will then jump to a farther ancestor.
Pitfall 7: layout effects of containment
Applying container-type is not free of side effects. It applies layout containment, style containment, and inline-size containment (or full size containment). That has visible consequences:
- The container becomes the containing block for absolutely and fixed-positioned descendants. A modal with
position: fixedinside a container is positioned relative to the container, not the viewport. - The container creates an independent formatting context, so margins of the first and last children no longer collapse through it.
- The container becomes a stacking context for
z-indexpurposes. - Counters and quotes are scoped to the container.
If a dropdown, tooltip, or modal suddenly clips or jumps after you added container-type, this is the cause. The fix is to move the container so it does not wrap overlay elements, or to render overlays outside the container.
Pitfall 8: cascade order and specificity
@container does not add specificity. A rule inside it has the same specificity as the same selector outside it, so source order decides. This is a very common mistake:
.card {
display: block;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}
/* Later rule with the same specificity wins at every size */
.card {
display: block;
}Put base styles before the container rules and keep the container rules after them. A more specific selector elsewhere, such as .layout .card, also beats a plain .card inside the query. DevTools shows the matching rule struck through when this is the case.
Pitfall 9: units and style queries
Container units such as cqi resolve against the nearest eligible container. If there is none, they fall back to the small viewport units (svi, svb, and so on). A heading that scales with the viewport instead of the container is the sign that no container was found. Give an element container-type: inline-size on an ancestor, and check that the unit is used on a descendant of it.
For style queries, remember three points:
style(--theme: dark)compares computed values of custom properties on the container, so--thememust be set on an ancestor (or inherited down to it) and not only on the target element.- Values are compared after computation, so the written form must match, for instance
darkandDarkare different. - Support for style queries on ordinary properties is not as widespread as for custom properties. Stick to custom properties.
Using DevTools
The browser developer tools are the fastest way to check the five conditions at the start of this chapter.
- Inspect the element you want to style and look at the Styles panel. A matching
@containerrule appears with its condition. If the rule is missing, no ancestor matched. - Look for the container badge in the Elements panel. Chromium and Firefox mark elements that are query containers. Click it to highlight the container.
- Select the container and check its computed width. A value near zero points to Pitfall 3.
- Resize the container, not the window. Resize the browser, or change the width of the parent, and watch which rule turns on and off.
When tools do not help, add a temporary indicator inside the container that prints the current state:
.card-wrapper {
container-type: inline-size;
outline: 2px dashed hotpink;
}
.card-wrapper::before {
content: "narrow";
display: block;
font: 12px/1 monospace;
}
@container (400px <= width < 700px) {
.card-wrapper::before {
content: "medium";
}
}
@container (width >= 700px) {
.card-wrapper::before {
content: "wide";
}
}This example has a problem you should recognize by now. The ::before pseudo-element belongs to the container itself, so the rules cannot style it, and the label stays "narrow". Move the label onto a child to make it work:
.card-wrapper {
container-type: inline-size;
outline: 2px dashed hotpink;
}
.card-wrapper > .debug::before {
content: "narrow";
font: 12px/1 monospace;
}
@container (400px <= width < 700px) {
.debug::before {
content: "medium";
}
}
@container (width >= 700px) {
.debug::before {
content: "wide";
}
}Add <div class="debug"></div> as the first child of .card-wrapper. Now the label always shows which branch is active.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Rule never applies | Target is the container itself | Query from a descendant, add a wrapper |
| Rule never applies | No `container-type` on an ancestor | Add `container-type: inline-size` |
| Stuck in the narrow layout | Container width collapsed | Give it a width, `flex-basis`, or grid track |
| Height query is ignored | Container uses `inline-size` | Use `size` and set a height |
| Wrong breakpoint | Nearest container is not the one intended | Use `container-name` |
| Modal or tooltip misplaced | Containment creates a containing block | Move overlays outside the container |
| Rule matches but loses | Later rule with the same specificity | Reorder rules |
| `cqi` acts like viewport units | No eligible container found | Add a container ancestor |
Exercise
A developer writes .panel { container-type: inline-size; } and @container (min-width: 500px) { .panel { padding: 2rem; } }. The .panel is a flex item with flex: none inside a flex row, and the padding never changes. Name two separate problems and give a fix for each.
Show answer
First, the rule styles the container itself. @container rules only apply to descendants of the container, so .panel cannot be both the container and the target. Wrap it, or style a child such as .panel > .body. Second, .panel is a flex item with flex: none and has inline-size containment, so its width cannot come from its content and it can collapse to near zero. Give it a size that does not depend on its children, for example flex: 1 1 20rem; min-width: 0;, so the query measures a meaningful width.
Exercise
You add container-type: inline-size to a wrapper around a page section. A position: fixed banner inside the section now sits in the wrong place. Why, and what are two ways to fix it?
Show answer
Container types apply layout containment, which makes the container the containing block for fixed and absolutely positioned descendants. The banner is positioned relative to the wrapper and not the viewport. Fix it by moving the banner outside the container element in the markup, or by choosing a smaller wrapper that does not contain the banner.