Sub topic 11
Pitfalls: Self-Styling, Inline-Size Collapse and Containment Effects
This chapter covers the ways container queries, container units and style queries fail in practice. It assumes you can write @container rules, declare container-type, and use cqi with clamp(). It also assumes you know custom properties and basic flexbox and grid sizing. Most bugs here come from three causes: a rule that targets the wrong element, a container whose width becomes zero, or side effects of the containment the container creates.
Pitfall 1: An element cannot query itself
A container query styles the descendants of a container. It never styles the container. The browser needs the container's size to evaluate the condition, and styling the container based on its own size could change that size and create a loop. The spec rules this out.
This code looks reasonable but does nothing:
.card {
container: card / inline-size;
}
@container card (min-width: 30rem) {
.card {
display: grid; /* never applies: .card is the container */
}
}The fix is to add a wrapper element. The wrapper is the container and .card is the styled descendant:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text.</p>
</article>
</div>.card-wrapper {
container: card / inline-size;
}
.card {
display: block;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}A single element can still be both a container and a styled element when the styled part is its child. If you control the markup, a wrapper costs little. If you do not, pick an existing ancestor that has a sensible size.
Units follow the same rule
Container query units on the container itself resolve against the next container above it. If there is none, they fall back to the small viewport units (cqi becomes svi, cqb becomes svb, cqw becomes svw, cqh becomes svh). cqmin and cqmax use the smaller or larger of the cqi and cqb results.
.card {
container: card / inline-size;
padding: 2cqi; /* resolves against an ancestor container, or svi if none */
}Put padding: 2cqi on a child of the container if you want it to track the container's width.
Style queries follow it too
A style query tests the computed custom properties of the container. Your own element's --tone does not match a rule on that same element. Set the property on an ancestor:
.alert-region {
--tone: warning;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
}
}If you write .alert { --tone: warning; } and then query style(--tone: warning) for .alert's own border, the query fails. Put the property on a parent, or style a child of .alert instead.
Pitfall 2: Inline-size collapse
container-type: inline-size applies inline-size containment. The browser must compute the element's inline size (its width in horizontal writing modes) without looking at its content. Otherwise the content's size could depend on a query result that depends on the content's size.
For a normal block element this is fine, because a block stretches to fill its parent. The problem appears when the element's width would normally come from its content (shrink-to-fit sizing). With containment, the content contributes zero to that width, so the container collapses to its padding and border.
Situations that collapse:
- A container that is a float or an
inline-block, orinline-flexorinline-grid, withwidth: auto. - An absolutely positioned container with
width: autoand noleft/rightpair. - A flex item with
flex: 0 1 autoandwidth: autoin a row flex container. - A grid item in a track sized with
autoormax-content. - A container inside a table cell or a shrink-wrapped parent.
Example of the flex case:
<div class="row">
<div class="sidebar">Sidebar</div>
<div class="card-wrapper">
<article class="card"><h2>Title</h2><p>Some content.</p></article>
</div>
</div>.row {
display: flex;
gap: 1rem;
}
.card-wrapper {
container: card / inline-size;
/* Collapses: width is auto, and contents contribute nothing. */
}The wrapper is a flex item with an automatic basis. Its content size is zero under containment, so it renders with no width. Any of these fixes work:
.card-wrapper {
container: card / inline-size;
flex: 1; /* grow into the free space */
min-width: 0; /* allow shrinking below content size */
}or give it an explicit width, flex-basis, or a grid track such as minmax(0, 1fr).
Why `size` is harsher
container-type: size contains both axes. The container's block size (height) also ignores its content, so with height: auto it collapses to zero height. Use size only when the container has a definite height, such as a fixed-height panel or a grid area. Use inline-size for the common case of width-driven layout.
This also decides which units work. In an inline-size container, cqi and cqw resolve against the container. cqb and cqh have no block axis to use, so they skip that container and look further up, or fall back to svb and svh. A heading sized with cqb inside an inline-size container uses the viewport. This is a quiet way to get wrong sizes.
Containers that do nothing
container-type has no effect when the element generates no suitable box:
display: inline(non-replaced inline boxes cannot take size containment). Usedisplay: blockorinline-blockwith an explicit width.display: contents, which has no box at all.- Internal table boxes such as rows and cells. Wrap the content in a
divinside the cell.
Pitfall 3: Containment side effects
Size containers also apply layout containment and style containment. These change how the element behaves toward the rest of the page, whether or not you notice.
| Effect | What changes | Typical symptom |
|---|---|---|
| Containing block for positioned descendants | `position: absolute` and `position: fixed` children position relative to the container | A fixed header or modal inside the container sticks to the container, not the viewport |
| Independent formatting context | Margins of children do not collapse through the container's edges | Extra gap appears above or below the first or last child |
| Stacking context | The container gets its own `z-index` layer | A dropdown inside it cannot appear above a sibling outside it |
| Style containment | Counters and similar scoped values start fresh inside the container | `counter-increment` inside does not continue numbering outside |
| Inline-size containment | Width ignores content | Collapse, as above |
A position: fixed modal inside a container is the most common surprise:
<div class="card-wrapper">
<dialog class="modal" open>Hello</dialog>
</div>.card-wrapper {
container: card / inline-size;
width: 20rem;
height: 10rem;
}
.modal {
position: fixed;
inset: 0; /* fills .card-wrapper, not the viewport */
}Because layout containment makes the container the containing block for fixed descendants, inset: 0 fills the 20rem by 10rem wrapper. To cover the viewport, render the modal outside the container, or use <dialog> with showModal(), which places it in the top layer and avoids this.
The stacking context is similar. A tooltip inside one container will never draw above a later sibling container with a higher stacking position. Move overlays to a higher level in the DOM, or raise the z-index of the whole container.
Pitfall 4: Shorthand and naming mistakes
The container shorthand sets container-name and container-type together. If you omit the type, it resets to normal:
.card-wrapper {
container-type: inline-size;
container: card; /* resets container-type to normal! */
}Now .card-wrapper has a name but no size containment, so size queries do not match it. Write container: card / inline-size, or set container-name as a longhand after the shorthand.
A named query only matches an ancestor with that name. A typo in the name means the rule never applies, and the browser gives no error. When a rule does nothing, check three things in order: is the name spelled the same, does the container have a type that supports the feature you query, and is the styled element a descendant of it.
Pitfall 5: Picking the wrong ancestor
An unnamed @container rule uses the nearest ancestor that can answer the question, so a size query skips ancestors that are not size containers. With nested containers, a rule inside an inner container uses the inner one by default. To reach the outer one, name it:
.page { container: page / inline-size; }
.sidebar { container: sidebar / inline-size; }
@container page (min-width: 60rem) {
.sidebar .link { font-size: 1.125rem; }
}Without the page name, .link would be tested against .sidebar. Container units have no name syntax and always use the nearest suitable container. If you need the outer container's width inside an inner container, store it in a custom property on the outer container's child or use a name-based query instead.
Pitfall 6: Style query limits and support
Style queries on custom properties are the part of the feature with the least consistent support across browsers. Support for style() also differs from support for size queries, so a browser that handles @container (min-width: ...) may ignore @container style(...). An unsupported query block is skipped as a whole.
Guard the enhancement and keep a safe default outside it:
.alert {
border: 2px solid gray; /* works everywhere */
}
@supports (container-type: inline-size) {
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
}
}
}@supports (container-type: inline-size) tests size containers. For style() there is no equally clean feature test, so design the default so that the unstyled version still looks correct. Use @supports to wrap size queries, and make style queries cosmetic, such as color and borders, rather than structural.
Two more points about style queries:
- Custom properties inherit, so a
--toneset on.alert-regionmatches for every descendant, not only direct children. - The comparison uses the computed value. If you register the property with
@property, the value is parsed against its syntax, which makes comparison more predictable than comparing plain text.
A quick checklist
- Is the styled element a descendant of the container, and not the container?
- Does the container have
container-type: inline-sizeorsize, and is it not reset by the shorthand? - Does the container have a width that does not come from its content?
- Is the container's
displayone that creates a box? - Does the name match, and is the intended ancestor the nearest suitable one?
- Could layout, stacking or fixed-position effects explain odd rendering inside it?
- Is there a default style for browsers without support?
Exercise
A developer writes .card { container: card / inline-size; } and then @container card (min-width: 30rem) { .card { display: grid; } }. The grid never appears. Give two ways to fix it.
Show answer
The rule targets the container itself, and a container cannot be styled by its own query. Fix 1: wrap the card in .card-wrapper, make the wrapper the container, and keep .card as the styled descendant. Fix 2: keep .card as the container and style its children (for example .card > .inner) in the @container rule.
Exercise
A container with container-type: inline-size is a flex item with width: auto and renders at zero width. Why, and what is a fix?
Show answer
Inline-size containment makes the element ignore its content when computing its width, so a shrink-to-fit width becomes zero. Give it a width from somewhere else, for example flex: 1 with min-width: 0, an explicit width or flex-basis, or a grid track like minmax(0, 1fr).
Exercise
Inside an inline-size container, a heading uses font-size: 5cqb. What does 5cqb resolve against, and why is this a bug risk?
Show answer
An inline-size container has no block-axis containment, so it does not answer cqb. The unit looks for a size container further up, or falls back to svb (a small viewport unit) if none exists. The heading then scales with the viewport or a distant ancestor instead of its own container. Use cqi, or switch the container to container-type: size and give it a definite height.
Exercise
A position: fixed; inset: 0 modal inside a size container covers only the container. Why, and how do you make it cover the viewport?
Show answer
Layout containment makes the container the containing block for fixed-position descendants. Render the modal outside the container in the DOM, or open it as a <dialog> with showModal() so it is placed in the top layer.