Sub topic 11
Pitfalls: self-styling, inline-size collapse and containment effects
This chapter assumes you can write @container size queries, declare containers with container-type, and use cqi units. It covers three mistakes that account for most "my container query does nothing" reports, and the side effects containment has on layout.
Pitfall 1: styling the container itself
An @container rule never matches the container that is being queried. The rule looks at the nearest ancestor container of the element it styles. If you write a rule for the container element, the browser looks above it instead.
.card-wrapper {
container: card / inline-size;
}
/* Broken: .card-wrapper cannot be styled by its own width */
@container card (min-width: 30rem) {
.card-wrapper {
padding: 2rem;
}
}This rule never applies. The fix is to add a child element and style that, or to restyle the wrapper from a different, outer container.
@container card (min-width: 30rem) {
.card {
padding: 2rem;
}
}The same rule applies to style queries. An element is never matched by a style() query on its own custom properties. Set the property on an ancestor, as .alert-region does in the previous chapter.
Pitfall 2: inline-size collapse
container-type: inline-size applies inline-size containment. The browser must compute the container's width without looking at its contents. If the width normally comes from the contents, it collapses, often to zero.
This happens when the container has no width of its own to fall back on:
- A flex item with
flex: noneorwidth: autoinside a row flex container. - A grid item in an
autotrack. - An
inline-block, a float, or an absolutely positioned element. - A table cell.
/* Risky: width would come from the contents */
.toolbar {
display: flex;
}
.toolbar .card-wrapper {
container-type: inline-size; /* may collapse to 0 */
}
/* Safe: give the container a width that does not depend on contents */
.toolbar .card-wrapper {
container-type: inline-size;
flex: 1 1 20rem;
min-width: 0;
}Block-level elements in normal flow are safe, because they fill the available width. Heights are unaffected by inline-size. If you use container-type: size, you also contain the block axis, so you must give the container an explicit height, or it collapses vertically. Prefer inline-size unless you need cqh or cqb to be meaningful.
Pitfall 3: containment side effects
Declaring a size container also applies layout and style containment. These have visible consequences.
| Effect | What happens |
|---|---|
| Containing block | The container becomes the containing block for absolutely and fixed-positioned descendants. A `position: fixed` modal inside it is now positioned relative to the container, not the viewport. |
| Stacking context | The container creates a new stacking context, so `z-index` values inside it cannot rise above siblings of the container. |
| Independent formatting context | Margins of children do not collapse through the container. |
| Style containment | Counters and quotes started inside do not leak out. |
| Inline size | The container's width ignores its contents, as described above. |
If a dropdown is suddenly clipped, mispositioned, or hidden under a neighbor, check whether an ancestor became a container.
/* Move the container to a wrapper that does not contain overlays */
.page-section {
container: section / inline-size;
}Place containers around the smallest component that needs them, and render overlays such as modals outside those containers in the DOM.
A debugging checklist
Browser dev tools help here. Select the container element and inspect its computed width. A value of 0px confirms a collapse.
<div class="toolbar">
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text.</p>
</article>
</div>
</div>With the safe CSS above, this markup gives .card-wrapper a real width, so .card can respond to it.
Summary of rules
- Style descendants of a container, never the container itself.
- Give each inline-size container a width that does not come from its contents.
- Use
inline-sizerather thansizeunless you also control the height. - Expect a new containing block and stacking context, and keep overlays outside containers.
Exercise
A .gallery is display: flex. Each .tile inside it has container-type: inline-size, and a rule @container (min-width: 20rem) { .tile { background: gold; } }. The tiles are zero-width and the rule never applies. Give two separate reasons the rule fails and one fix for each.
Show answer
First, the rule styles .tile, which is the container, so it is matched against an ancestor container (or none) and never against itself. Fix: move container-type to a wrapper and style the .tile inside it, or style a child of .tile. Second, a flex item with auto width has its width taken from its contents, and inline-size containment ignores contents, so it collapses to zero. Fix: give the tile a content-independent width such as flex: 1 1 20rem; min-width: 0;.