Sub topic 2
The container query idea
This chapter assumes you know the cascade, flexbox or grid, and how @media rules work. It introduces the core idea of container queries. The syntax is covered in more detail in the chapters that follow.
The problem in one picture
Suppose you build a card with an image beside the text. In a wide main column it looks good. Put the same card in a narrow sidebar and the two columns get cramped.
A viewport media query can't fix this. It only knows the width of the browser window. On a 1400px-wide screen, the main column and the sidebar both see "1400px", even though the card has very different amounts of room in each place. You can patch around it with extra classes such as .card--narrow, but then the page, not the component, decides how the component looks.
The idea
A container query lets a component ask about the size of one of its ancestors instead of the viewport. That ancestor is the query container. The component's rules adapt to the space it was given, wherever it is placed.
Two pieces are always involved:
- An ancestor element that you mark as a query container.
- An
@containerrule that applies styles to descendants of that ancestor when a size condition is true.
A first look
The next chapter explains the declaration in detail. For now, read this example for its shape.
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}.card-wrapper becomes a query container. The card stays a single column by default. When the wrapper is at least 400px wide, the card switches to two columns. If you place this wrapper in a 300px sidebar, the card stays stacked. If you place it in a 700px column, the card becomes two columns. The viewport is the same in both cases.
How this changes your thinking
- The container and the styled element are different elements. An
@containerrule styles descendants of the container, never the container itself. That is why the example has both.card-wrapperand.card. If you style the element you marked as the container, nothing happens. - The component carries its own rules. The card's layout logic lives next to the card. Any page can drop it in without knowing its breakpoints.
- The condition is re-evaluated on resize. If the container grows or shrinks, for example when a sidebar collapses, the rule applies or stops applying. The window does not need to resize.
- Media queries still have a job. Use them for things that really depend on the viewport or the user's environment: the overall page layout,
prefers-reduced-motion, or print styles. Use container queries for components.
| Row | Media query | Container query |
|---|---|---|
| Measures | The viewport (or device features) | A chosen ancestor element |
| Needs setup on an ancestor | No | Yes, the ancestor must be a container |
| Result for the same component in two places | Identical | Can differ per place |
| Best for | Page-level layout, user preferences | Reusable components |
Keep in mind that marking an element as a container is a real change, not a free annotation. It affects how that element is sized and laid out. The chapter on containment side effects covers this, so don't add container-type to every element as a precaution.
Exercise
A .card has container-type: inline-size set on itself, and an @container (min-width: 400px) rule changes .card's padding. The card is 600px wide, but the padding never changes. What is the likely cause, and how do you fix it?
Show answer
An @container rule only styles descendants of the container, not the container itself. The card is querying itself, so the rule never matches. Add a wrapper element, put container-type on the wrapper, and keep the @container rule styling .card inside it.