Sub topic 3
Declaring a Container with container-type
This chapter assumes you know core CSS (selectors, the cascade, the box model), basic flexbox or grid layout, and how @media queries work. It covers the container-type property: what each value does, what it costs you in layout, and the errors you are most likely to hit. The @container rule itself comes in the next chapter, so the examples here use only a small, simple rule.
What container-type does
A container query asks about the size of an ancestor element. CSS cannot ask about the size of just any ancestor, because most elements get their size from their content. If a child's styles could depend on the parent's size, and the parent's size depends on the child's content, the browser could loop forever.
container-type breaks that loop. When you set it on an element, you tell the browser two things:
- This element is a query container. Descendants can ask about its size.
- The browser may apply size containment on the chosen axes. The element's size on those axes can no longer depend on what is inside it.
The second point is the price of the first. Because the container's size does not depend on its children, the children can safely react to it.
The three values
| Value | Queryable axes | Containment applied | Typical use |
|---|---|---|---|
| `normal` | None for size (style queries still work) | None | The default. The element is not a size container. |
| `inline-size` | Inline axis (width in horizontal writing modes) | Inline-size containment, layout containment, style containment | Most components |
| `size` | Both inline and block axes | Size containment on both axes, layout containment, style containment | Fixed-size panels, where height is also known |
normal
Every element starts as container-type: normal. It is not a size query container, so @container size conditions never match against it. It can still be a target for style queries, which a later chapter covers.
inline-size
This is the value you will use most. It makes the element a container for queries on its inline axis. In English and other horizontal writing modes, the inline axis is the horizontal one, so you query the width.
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}The wrapper becomes a query container. The card switches to two columns when the wrapper is at least 400px wide, whatever the viewport size. Notice that the query is on .card, not on .card-wrapper. An element cannot be styled by a query against itself. This point returns in the pitfalls section.
Inline-size containment means the container's width no longer comes from its content. A block-level container fills the available width like any block box. A container in a flex or grid layout gets its width from that layout, not from the text inside.
The block size is different. The height still comes from the content, as usual. That is why inline-size is practical: a card can still grow taller when its text wraps.
size
container-type: size contains both axes. You can then query height as well as width, but the element's height can no longer come from its content either. If you do not give the element a height through other means, it collapses to zero height on the block axis.
.panel {
container-type: size;
height: 24rem;
}Use size only when the container's height is set by something outside it: an explicit height, a grid track with a fixed size, a flex item that stretches, or aspect-ratio. If you set size on a wrapper whose height was content-based, the wrapper shrinks to nothing and its content overflows visibly.
Writing modes
"Inline" and "block" follow the writing mode, not the physical directions. In a vertical writing mode such as writing-mode: vertical-rl, the inline axis is vertical. Then container-type: inline-size contains the height, and width conditions would refer to the block axis, which is not contained. For this reason, newer logical conditions such as inline-size and block-size exist, and you will meet them in the chapter on size conditions. For horizontal text, inline-size and width line up.
What containment does to your layout
Applying container-type is not a purely passive label. The containment it turns on changes how the element is laid out. The next chapter on containment side effects goes deeper, but you need the main points now because they explain most surprises.
Size containment
With inline-size, the browser lays out the element as if it had no content when computing its inline size. Three consequences follow:
- A container with
width: autothat is shrink-to-fit gets a different size. A floated element, an absolutely positioned element, or a flex item withwidth: autocan become much narrower, because its content no longer contributes to its width. min-contentandmax-contentsizes of the container ignore the children's inline contribution.- The element still has its normal block size, based on its content, when the type is
inline-size.
Layout containment
Layout containment makes the container a few more things:
- It becomes a containing block for absolutely and fixed-positioned descendants. A
position: fixedchild is now positioned relative to the container, not the viewport. - It creates a new stacking context.
- It creates an independent formatting context, so floats inside do not escape and margins of children do not collapse through it.
The fixed-position effect is the one that surprises people most. A modal or toolbar placed inside a container no longer sticks to the viewport.
Style containment
Style containment scopes some features, such as counters and quotes, to the container. In practice it rarely matters for container queries.
A runnable example
This page shows the same card in a wide container and a narrow one. Both are on the same page, with the same viewport.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>container-type demo</title>
<style>
.card-wrapper {
container-type: inline-size;
border: 2px dashed gray;
margin-bottom: 1rem;
}
.wide { width: 40rem; }
.narrow { width: 16rem; }
.card {
display: grid;
gap: 1rem;
padding: 1rem;
}
.card img {
width: 100%;
height: 6rem;
background: steelblue;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
</style>
</head>
<body>
<div class="card-wrapper wide">
<article class="card">
<img alt="" src="data:image/gif;base64,R0lGODlhAQABAAAAACw=">
<div><h2>Wide card</h2><p>Two columns here.</p></div>
</article>
</div>
<div class="card-wrapper narrow">
<article class="card">
<img alt="" src="data:image/gif;base64,R0lGODlhAQABAAAAACw=">
<div><h2>Narrow card</h2><p>One column here.</p></div>
</article>
</div>
</body>
</html>The first card has a 40rem wrapper, which is well over 400px at a default font size, so it uses two columns. The second wrapper is 16rem, about 256px, so the card stays in one column. Resize the browser window: neither card changes, because the rule depends on the wrapper, not the viewport.
Choosing between inline-size and size
Default to inline-size. It covers the most common need, which is reacting to available width, and it leaves height flexible. Reach for size only if both of these hold:
- You need conditions on height.
- The container's height is already fixed by something other than its content.
Applying size to many elements "just in case" will cause collapsed layouts. Applying it only where needed keeps containment's side effects limited.
Common errors
- Putting the query on the container itself. A rule such as
.card-wrapper { ... }inside@containernever matches the wrapper that declares the container. Query rules style descendants. Add an inner element, or move the declaration to a parent. - Setting
container-type: sizeand losing the height. The container collapses. Give it an explicit height or useinline-size. - Expecting a shrink-to-fit container to size to its content. An inline-size container in a float, a flex row with
width: auto, or an inline-block no longer widens to fit its children. Set an explicit width, or useflex: 1, or place it in a block that has a width. - Forgetting the new containing block. A fixed-position child now positions against the container. Move the child outside the container if it must be viewport-fixed.
- Declaring the container on an element with
display: inline. Size containment does not apply to inline boxes, so the element does not behave as a size container. Usedisplay: block,flex,grid, orinline-block.
Exercises
Exercise
You set container-type: inline-size on a div whose height previously came from its text. Does the div collapse to zero height? What changes if you use container-type: size instead?
Show answer
With inline-size, the div keeps its content-based height, because only the inline axis is contained. With size, both axes are contained, so the block size no longer comes from the content and the div collapses (to zero height unless it has an explicit height or another outside source of size).
Exercise
A position: fixed toolbar sits inside an element you just gave container-type: inline-size. It stops staying at the top of the viewport. Why, and what are two ways to fix it?
Show answer
Layout containment makes the container the containing block for fixed-position descendants, so the toolbar positions against the container. Fix it by moving the toolbar outside the container element, or by putting the container on a different, lower element so the toolbar is not its descendant.
Exercise
This CSS never changes the wrapper's padding. Explain why. .box { container-type: inline-size; } @container (min-width: 300px) { .box { padding: 2rem; } }
Show answer
A container query matches ancestors of the styled element, and an element cannot query itself. .box is the container, so the rule has no ancestor container to read. Wrap .box in a parent that declares container-type, or style an element inside .box.