Sub topic 8
What containment does to your layout
This chapter assumes you can already declare a container with container-type and write a basic @container rule. It also assumes you know the box model, flexbox, grid, and the difference between normal flow and out-of-flow positioning. Here you will learn the side effects of container-type, because it changes how the element is sized and laid out. Most "my container query broke my page" reports trace back to the effects below.
Why a container must be contained
A container query asks "how wide is this ancestor?" The browser can only answer if the ancestor's size does not depend on the styles the query applies to its descendants. Otherwise you get a loop:
- The query matches, so the child gets wider.
- The container grows to fit the child.
- The container is now wide enough that the query no longer matches.
- The child shrinks, and the cycle repeats.
CSS breaks the loop with containment. When you set container-type, the browser applies a set of containment rules to that element. The element's size is computed without looking at its descendants, in at least one axis. The side effects come from that rule.
What each container-type applies
| Value | Containment applied | Size ignores content in |
|---|---|---|
| `normal` (initial) | Style containment only for style queries, no size containment | Neither axis |
| `inline-size` | Layout, style, and inline-size containment | Inline axis (width in horizontal writing modes) |
| `size` | Layout, style, and size containment | Both axes |
You can query width and inline-size with inline-size. You need size only to query height or block-size, and it has the largest side effects. Prefer inline-size unless you need height queries.
Containment does not apply to every box. Elements with display: contents generate no box, so they cannot be containers. Non-atomic inline boxes (a plain display: inline element) and internal table boxes (rows, row groups, cells) cannot be size containers either. Use a block, flex, grid, inline-block, or table-wrapper box.
Side effect 1: the width no longer comes from the content
With inline-size containment, the container's width is calculated as if it had no content. In most layouts this changes nothing, because a block-level element fills its parent's width regardless of content. It matters when the width would normally come from the content (shrink-to-fit sizing).
Shrink-to-fit happens in these places:
- Floated elements with
width: auto. display: inline-blockandinline-flexelements.- Absolutely positioned elements with
width: autoand no both-sided offsets. - Flex items with
flex-basis: autoandwidth: autoin a row flex container. - Grid items in
autotracks.
In each case, a container with no explicit width collapses to zero width. Here is the classic failure:
.row {
display: flex;
gap: 1rem;
}
/* Broken: each wrapper's width is computed as if it were empty. */
.row > .card-wrapper {
container-type: inline-size;
}Each .card-wrapper shrinks to zero wide and its content overflows or becomes invisible. Give the item a size that does not come from its content:
.row {
display: flex;
gap: 1rem;
}
/* Fixed: the flex algorithm now decides the width. */
.row > .card-wrapper {
container-type: inline-size;
flex: 1 1 0;
min-width: 0;
}The same fix applies elsewhere: set width, flex, or inline-size on a float or inline-block, or use 1fr and minmax() tracks in grid.
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}
.gallery > .item {
container-type: inline-size;
}The grid above works because minmax(16rem, 1fr) gives each track a size without consulting the item's content. A track declared as auto would not.
Side effect 2: height collapses with container-type: size
container-type: size applies size containment in both axes. The container's height is computed as if it were empty, so an element with height: auto becomes 0 tall. You must provide a height yourself:
.panel {
container-type: size;
height: 20rem;
}
@container (height > 15rem) {
.panel-body {
columns: 2;
}
}Without the explicit height, the container has zero height, and the height > 15rem condition never matches. If you only need width-based changes, use inline-size so the height keeps growing with the content.
Side effect 3: the container is a containing block for positioned descendants
Layout containment makes the container the containing block for absolutely positioned and fixed-position descendants. You do not need position: relative for this. This can move things unexpectedly:
.page-header {
position: fixed;
top: 0;
inset-inline: 0;
}
.card-wrapper {
container-type: inline-size;
}A position: fixed element placed inside .card-wrapper no longer sticks to the viewport. It is positioned relative to the wrapper, like transform does. A modal, tooltip, or dropdown that previously escaped to the viewport (or to a distant position: relative ancestor) now anchors to the container. If you see an overlay jump to the wrong spot after adding a container, move the overlay outside the container in the DOM or put the container on a different element.
Side effect 4: an independent formatting context
Layout containment makes the container establish an independent formatting context, similar to display: flow-root. Three consequences follow:
- Margins stop collapsing through the container. A first child's
margin-topthat used to merge with the container's own margin now stays inside, which can add visible space. - Floats are contained. Floated children no longer escape the container's bottom edge, so no clearfix is needed. This is often an improvement.
- The container creates a stacking context. Descendants with
z-indexare ordered inside it and cannot interleave with elements outside. A highz-indexmenu inside a container can be covered by a lower one outside.
.card-wrapper {
container-type: inline-size;
}
.card h2 {
margin-block-start: 2rem; /* now stays inside .card-wrapper */
}Side effect 5: style containment scopes counters
The style containment applied here scopes CSS counters and the quotes behavior to the container's subtree. A counter-increment inside the container does not carry over to the same counter name outside it. If you number headings across a page and wrap some of them in containers, numbering inside the container restarts from the container's own scope instead of continuing the page's sequence. Place the container so it wraps the whole counted region, or keep counter-bearing elements outside the container.
A container cannot query itself
Containment does not change this rule from earlier chapters. @container rules style descendants of the container, never the container itself. If you write .card-wrapper { container-type: inline-size; } and then @container (min-width: 400px) { .card-wrapper { padding: 2rem; } }, the padding never applies. The usual pattern is a wrapper that is the container and an inner element that gets styled.
Checklist before shipping a container
- Does the container's width come from a normal block fill, a flex rule such as
flex: 1, a grid track with a definite size, or an explicitwidth? - Do you really need
size? If not, useinline-size. - Are there fixed or absolute descendants (menus, tooltips, modals) that depended on a distant ancestor?
- Does any first child rely on margin collapsing with its parent?
- Do any
z-indexvalues need to interact with elements outside the container? - Does a CSS counter span the container's boundary?
Exercise
A page shows three cards in a row using display: flex. After you add container-type: inline-size to each .card-wrapper flex item, the cards vanish. Why does this happen, and what is a minimal fix?
Show answer
The wrapper's flex-basis is auto and its width is auto, so its width would come from its content. Inline-size containment makes the browser compute that width as if the container were empty, giving zero width. Give each wrapper a size source that does not depend on its content, for example flex: 1 1 0; min-width: 0; (or an explicit width).
Exercise
You set container-type: size on a .panel with height: auto, and a rule @container (height > 15rem) never matches. Name the cause and two ways to fix it.
Show answer
Size containment ignores the content's height, so the panel's auto height is 0 and the condition cannot be true. Either set an explicit height (such as height: 20rem) or a height from the surrounding layout, or switch to container-type: inline-size if you only need width-based queries.
Exercise
A position: fixed toast inside a card used to stick to the bottom of the viewport. After you made the card's wrapper a container, it sits at the bottom of the wrapper. Explain why, and name one fix.
Show answer
Layout containment makes the container the containing block for fixed and absolute descendants, so the toast is positioned relative to the wrapper and not the viewport. Move the toast outside the container in the DOM, or apply container-type to an element that does not contain the toast.