Sub topic 2
Declaring a container with container-type
This chapter assumes you know CSS selectors and the cascade, media queries, and basic flexbox or grid. You will learn how to turn an element into a query container, what that costs, and which container-type value to choose.
What a container is
A media query asks about the viewport. A container query asks about an ancestor element. For that to work, the browser must know which ancestor you mean. By default, no element is a query container. You opt in with the container-type property.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>.card-wrapper {
container-type: inline-size;
}Here .card-wrapper is the container. Later chapters write @container rules against it. The .card is the descendant those rules style.
The three values
| Value | Queryable dimensions | Containment applied |
|---|---|---|
| `normal` (initial) | None for size. Style queries still work. | None |
| `inline-size` | Inline axis (width in horizontal writing modes) | Layout, style, inline-size |
| `size` | Both inline and block axes | Layout, style, size |
Use inline-size in most cases. It lets descendants react to available width, which is what most components need. Use size only when you also query height, and then you must give the element an explicit height.
Why containment is required
Suppose a card's width depended on its content, and its content changed layout depending on the card's width. The browser could loop forever. Containment breaks that loop. With inline-size, the browser computes the container's inline size without looking at its children. Children can then be styled from that size safely.
A container cannot style itself
A query container is never matched by its own @container rule. Only its descendants are. This is the most common first mistake.
/* Wrong: the rule targets the container itself */
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 30rem) {
.card-wrapper {
display: grid; /* never applies */
}
}The fix is to add a wrapper element, as in the HTML above. The outer element is the container and the inner element is styled.
Side effects of containment
Setting container-type to anything other than normal changes how the element behaves, even before you write any query.
- Layout containment makes the element a containing block for absolutely and fixed-positioned descendants. It also creates a stacking context.
- Style containment scopes counters and quotes to the element.
- Inline-size containment makes the element ignore its content when computing its inline size.
The last point causes the most trouble. A block-level element such as a div fills its parent's width anyway, so nothing changes. An element that sizes itself from its content can collapse to zero width. Examples are a flex item without flex-grow, a grid item in an auto column, and an inline-block.
.row {
display: flex;
}
/* This item may collapse to 0 width because its content no longer
contributes to its size. Give it an explicit width source. */
.row > .card-wrapper {
container-type: inline-size;
flex: 1;
}With flex: 1 the item gets its width from the flex algorithm instead of from its content, so it no longer collapses.
A complete example
The following rule switches the card to two columns once its wrapper is 30rem wide. The viewport size does not matter.
.card-wrapper {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}Put the same card in a narrow sidebar and in a wide main column. It uses the block layout in the first and the grid layout in the second, from one stylesheet.
Summary of decisions
- Pick the element that represents the available space for the component. A wrapper around the component is usually right.
- Set
container-type: inline-sizeon it. - Style only descendants in the
@containerrules. - Confirm the container still gets its width from its parent or layout.
Exercise
A developer sets container-type: inline-size on a .panel and writes @container (min-width: 40rem) { .panel { padding: 2rem; } }. The padding never changes. Give the cause and a fix. Then say what could go wrong if .panel is a flex item with no flex or width set.
Show answer
A container cannot match its own @container rule, so .panel is never styled by the query. Wrap the content in an inner element (for example .panel-body) and style that instead, or make .panel the inner element and put the container on a new parent. If .panel is a flex item sized from its content, inline-size containment removes its content from the width calculation, so it can collapse to zero width. Give it a width source such as flex: 1 or an explicit width.