Sub topic 1
Why components should respond to their container
This chapter explains the problem container queries solve and gives the working model you will use in the rest of the tutorial. The later chapters cover the syntax, units, and style queries in detail.
Prerequisites
This chapter assumes you already know:
- CSS selectors and the cascade.
- Media queries and viewport units such as
vwandvh. - CSS custom properties (
--namedeclarations andvar()). - Basic flexbox or grid layout.
- The
clamp()function.
The problem with viewport media queries
A media query like @media (min-width: 40rem) asks about the viewport. That works for page-level decisions, such as whether to show a sidebar. It works badly for reusable components.
Take a card with an image beside the text. On a wide screen, you might place that card in a narrow sidebar and also in a wide main column. The viewport is the same in both places, but the space available to the card is very different. A viewport query gives both cards the same layout, so one of them looks wrong.
You can work around this in two ways, and both have costs:
- Write extra selectors such as
.sidebar .cardthat override the layout. These couple the component to where it is placed. - Add modifier classes such as
.card--compactby hand. These must be kept in sync with the page layout.
A component that depends on its placement is not really reusable. Container queries fix this by letting the component ask about the size of the box it sits in.
The working model
Container queries use three parts:
- A container: an ancestor element that you mark with
container-type. - A query: an
@containerrule with a condition, such as a minimum width. - A descendant: an element inside the container that the rule styles.
The browser measures the container, evaluates the condition, and applies the styles to matching descendants. If the container is resized, the styles update.
Here is the markup. The wrapper is the container, and the article is the styled descendant.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The CSS marks the wrapper as a container and changes the card's layout when the wrapper is at least 30rem wide.
.card-wrapper {
container-type: inline-size;
container-name: card;
}
.card {
display: block;
}
@container card (min-width: 30rem) {
.card {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}container-type: inline-size tells the browser to track the container's width in horizontal writing modes. container-name: card gives the container a name, so the rule can target it. If you put the same markup in a 20rem sidebar, the card stays a block. In a 45rem main column, it becomes a two-column grid. The CSS did not change, and neither did the viewport.
Two additions you will meet later
Container queries give you two more tools, and later chapters cover both:
- Container query units such as
cqiandcqw. These are lengths measured against the container, so a heading can scale with its wrapper. - Style queries with
@container style(...). These match on the computed value of a custom property on an ancestor, so a component can switch variants or themes.
Both depend on the same idea: the component looks at an ancestor, not at the page.
Common errors at this stage
- Styling the container itself. A container query can only style descendants of the container. A rule such as
@container (min-width: 30rem) { .card-wrapper { ... } }never matches, because the wrapper is the element being measured. Put the container on a parent and style the child. - Forgetting
container-type.container-namealone does not make a size container. Without acontainer-type, the size query has nothing to measure. - Assuming the viewport still matters. Inside
@container,min-widthrefers to the container. It is easy to read it as a viewport condition when you skim.
Exercise
A card is placed in a 20rem sidebar and in a 45rem main column on the same page. The card uses @media (min-width: 30rem) to switch to a two-column layout. Which layout does each card get on a 1200px-wide viewport, and what change makes each card follow its own space?
Show answer
Both cards get the two-column layout, because the media query checks the viewport, which is wider than 30rem in both cases. To make each card follow its own space, set container-type: inline-size on the card's wrapper and replace the media query with @container (min-width: 30rem). The sidebar card then stays a single block, and the main-column card becomes a two-column grid.