Sub topic 4
Writing your first @container rule
This chapter assumes you know selectors, flexbox or grid, and @media queries. It also assumes you have read the previous chapter, where container-type turns an element into a query container. Here you write the rule that reads that container's size.
The shape of an @container rule
An @container rule looks like an @media rule. It has a condition in parentheses and a block of ordinary style rules inside. The difference is what the condition measures. @media (min-width: 400px) checks the viewport. @container (min-width: 400px) checks the width of the nearest ancestor that is a query container.
The rule works in two steps:
- An ancestor declares
container-type: inline-size, which makes it a query container. - An
@containerrule applies styles to elements inside that container when the condition is true.
A complete example
Here is the markup.
<div class="card-wrapper">
<article class="card">
<img src="photo.jpg" alt="A mountain lake" width="128" height="128">
<div>
<h2>Mountain lake</h2>
<p>A short description that sits beside the image when there is room.</p>
</div>
</article>
</div>Here is the CSS.
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}The wrapper is the query container. The .card is a child of the wrapper, so the rule can restyle it. By default the card is one column, with the image above the text. When the wrapper is at least 400px wide, the card becomes two columns.
Put this card in a narrow sidebar and it stays stacked. Put the same card in a wide main column and it goes side by side. The viewport size makes no difference, and that is the point of container queries.
What the rule can and cannot style
An @container rule never styles the container itself. It styles only the container's descendants. In the example, putting container-type: inline-size on .card and then styling .card inside the @container block would do nothing. The card would be querying itself.
This is why the example uses a wrapper. The wrapper holds the size, and the card responds to it. If you do not want an extra element in your markup, use a parent that already exists, such as a <li>, a grid cell, or a <section>.
Why `inline-size`
container-type: inline-size makes the container report its width in horizontal writing modes. Width is usually what you want, because a block's height normally comes from its content. The browser can then compute the container's width without looking at its children, which avoids circular dependencies. The next chapter covers conditions on other dimensions.
Common errors
- Forgetting
container-type. Without it, no ancestor is a query container, and the rule never matches. Nothing is reported as an error. - Styling the container itself. The rule applies only to descendants. Move the declaration to a child element.
- Querying a container whose width is not set by its parent. An
inline-sizecontainer ignores its children when sizing itself. An inline-level or shrink-to-fit container can collapse to nearly zero width. Use a block-level element, such as adivorsection, that fills its parent. - Mixing up units.
min-width: 400pxcompares against the container's content box width, not the viewport and not the border box.
Exercise
A developer sets container-type: inline-size on .card and writes @container (min-width: 400px) { .card { padding: 2rem; } }. The padding never changes, even when the card is wider than 400px. Why, and how do you fix it?
Show answer
An @container rule only styles descendants of the container, never the container itself. Here .card is both the container and the styled element, so the rule has no effect. Put container-type: inline-size on a parent such as a wrapper element, and keep .card as the child that the rule styles.