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 @media queries. You will learn how to turn an element into a query container, what each container-type value does, and what changes in your layout when you apply it.
Why an element must opt in
A component cannot ask "how wide is my parent?" by default. The browser would have to size the parent from its children, and then restyle the children based on that size. That is a circular dependency.
CSS breaks the cycle by requiring you to declare a container first. A declared container promises that its own size does not depend on its descendants in the queried axis. With that promise, the browser can measure the container, then style the children.
The container-type property
You set container-type on the element that will be measured. It takes three useful values.
| Value | What you can query | Containment applied |
|---|---|---|
| `normal` | Style queries only. This is the default. | None for size |
| `inline-size` | Width in horizontal writing modes (the inline axis) | Inline-size containment |
| `size` | Both width and height | Size containment on both axes |
For most components, use inline-size. Pages usually grow in height with their content, so height rarely makes a stable query.
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}Here .card-wrapper is the container. The .card inside it switches to two columns when the wrapper is at least 400px wide, whatever the viewport size.
Query the container's ancestors, not the container
A container query styles descendants of the container. It never styles the container itself. In the example above, a rule inside @container that targets .card-wrapper would have no effect. This is why the examples use two elements: a wrapper that is measured and a child that is styled.
What containment does to your layout
Applying container-type also applies CSS containment. This has visible side effects.
- Inline-size containment. With
inline-size, the container's width is computed as if it had no content. A container that relied on its children to set its width, such as a shrink-to-fit flex item or a floated element, can collapse to a width of zero. Give it an explicit width, or place it where it stretches, such as a block-level element or a grid track. - Size containment. With
size, both axes ignore content. A container without an explicit height collapses to zero height. Only usesizewhen you set the height yourself. - Layout and style containment. The container becomes a containing block for absolutely and fixed-position descendants, and it establishes an independent formatting context. Margins of children no longer collapse through it.
.card-wrapper {
container-type: inline-size;
width: 100%; /* avoids a zero-width collapse inside a shrink-to-fit parent */
}Height queries need size
Querying height only works on a container with container-type: size. On an inline-size container, a height condition never matches.
.panel {
container-type: size;
height: 24rem; /* required: size containment ignores content height */
}
@container (height > 500px) {
.panel-body {
padding: 2rem;
}
}Common mistakes
- Putting
container-typeon the element you want to style instead of on an ancestor. - Using
sizeand forgetting to set a height. - Setting
container-type: inline-sizeon an element whose width comes from its content. - Expecting
normalto support size conditions. It does not.
Exercise
A .sidebar element has container-type: inline-size, and you write @container (min-width: 300px) { .sidebar { background: lightblue; } }. The sidebar is 400px wide, but the background never changes. Why, and how do you fix it?
Show answer
A container query only styles descendants of the container, never the container itself. Move the background rule to a child, for example .sidebar-inner, or wrap .sidebar in a new element that holds container-type: inline-size and style .sidebar from there.
Exercise
You set container-type: size on a div and its content disappears from the page flow, leaving a zero-height box. What caused this, and what is the smallest fix?
Show answer
Size containment makes the container ignore its content when computing height, so with no explicit height it collapses to zero. Give it a height, such as height: 20rem, or switch to container-type: inline-size if you only need width queries.