Sub topic 13
Debugging and common pitfalls
This chapter assumes you can write a basic @container rule, know what container-type and container-name do, and are comfortable with flexbox and grid. When a container query does nothing, the cause is almost always one of a few things. Check them in order.
The checklist
Pitfall 1: no container, or the wrong element
A query only matches if some ancestor declares container-type. If none does, the rule is silently ignored.
A query also cannot style the container itself. It styles only the container's descendants.
.card-wrapper {
container-type: inline-size;
}
/* Never applies: the container cannot restyle itself */
@container (min-width: 400px) {
.card-wrapper {
padding: 2rem;
}
}
/* Works: .card is a descendant of the container */
@container (min-width: 400px) {
.card {
padding: 2rem;
}
}If you need to change the wrapper, add an inner element and style that.
Pitfall 2: querying height without `size`
container-type: inline-size lets you query only the inline dimension, which is width in horizontal writing modes. A height condition then never matches. Use container-type: size for that. Also give the element an explicit height, because size containment ignores the contents when computing height.
Pitfall 3: the container collapses to zero width
Containment tells the browser to ignore the container's contents when computing its size along the contained axis. If the container's width came from its contents, that width becomes zero.
This hits elements whose width is shrink-to-fit: flex items with flex: none or auto, floats, inline-blocks, and auto-sized grid columns.
.layout {
display: flex;
}
/* Collapses: width would have come from the content */
.card-wrapper {
container-type: inline-size;
}
/* Fixed: the wrapper gets its width from the layout */
.card-wrapper {
container-type: inline-size;
flex: 1;
min-width: 0;
}A block-level element in normal flow is fine, because it already fills its parent.
Pitfall 4: names and nearest-ancestor matching
An unnamed @container rule matches the nearest ancestor that has any container type. It does not look further up if that ancestor fails the condition. A named rule skips ancestors that do not have that name.
- An unnamed rule inside a small inner container never "falls through" to a wide outer one.
- A typo in
container-name, or in the name in the rule, means no match and no warning. - The shorthand
container: sidebar / inline-sizeputs the name first, then a slash, then the type. Swapping them is invalid.
If a rule picks the wrong container, give the intended one a name and use that name in the rule.
Pitfall 5: side effects of containment
Setting container-type also applies containment to the element. Watch for these effects:
- Layout containment makes the container a containing block for absolute and fixed descendants. A
position: fixedchild can suddenly position against the container. - The container becomes a stacking context, so
z-indexbehavior may change. - Margins of children no longer collapse through the container.
- Style containment scopes counters to the container.
If a layout shifts right after you add container-type, suspect these before suspecting your query.
Pitfall 6: units and style queries
cqi and cqw resolve against the nearest container that supports the matching axis. With no such container, they fall back to the small viewport units (svi, svw). A heading that scales with the viewport instead of the container usually means the container is missing.
A style query such as @container style(--theme: dark) compares computed values of the nearest ancestor. The custom property must be set on an ancestor of the styled element, not on a rule nested inside a query for that same element.
Inspecting in the browser
Chromium and Firefox DevTools mark container elements with a container badge in the Elements panel. Click it to highlight the container. Use the Computed pane on the container to check its width, and the contain value to confirm that container-type took effect. A width of 0px points to Pitfall 3.
Exercise
A .card-wrapper has container-type: inline-size, and it sits in a flex row with the default flex: 0 1 auto. Your @container (min-width: 400px) rule never matches, and DevTools reports the wrapper's width as 0. Why, and what is a minimal fix?
Show answer
Inline-size containment makes the browser ignore the wrapper's contents when computing its width. A flex item with flex-basis: auto takes its width from its contents, so it shrinks to 0. Give it a width from the layout, for example flex: 1; min-width: 0; or width: 100%.
Exercise
Why does @container (min-width: 400px) { .card-wrapper { padding: 2rem; } } have no effect when .card-wrapper is the container?
Show answer
A container query matches descendants of the container, never the container itself. Apply the padding to a child such as .card.