You put the same product card in two places: in the wide main column and in a narrow sidebar. In the main column, the photo to the left of the title looks right. In the sidebar, the same arrangement has too little room. CSS can check the screen width, but that doesn't help here, because the screen is the same. Only the place where the card sits is different.
A container query lets a component (a piece of the page, like a card) change its look to fit the width of its wrapper, not the width of the screen. The wrapper is the box around the component: its parent. The content of the same card can stack in a narrow sidebar and sit side by side in a wide main column, with no extra CSS for each place. The card is like a folding table, and its wrapper is like a room: you open the table wide in a big room, and fold it in a small corner.
A card that follows its wrapper's width
This example has one card (.card) inside a wrapper (.wrapper), then the CSS:
<div class="wrapper">
<div class="card">
<img src="photo.jpg" alt="Product photo">
<p>Card title</p>
</div>
</div>
<style>
.wrapper {
container-type: inline-size;
}
@container (min-width: 350px) {
.card {
display: flex;
gap: 16px;
}
}
</style>
container-type: inline-sizemakes.wrappera container: a box whose width your CSS can ask about.inline-sizemeans its width is what gets measured.@container (min-width: 350px)is the question: "is the nearest container at least 350px wide?" The nearest container is the closest wrapper around.cardthat hascontainer-type, here.wrapper.- The
.card { ... }rule inside the curly braces of@containeronly applies when the answer is yes.
The result: while .wrapper is narrower than 350px, the rules inside @container are not used. The photo and the title stack, photo on top. As soon as .wrapper is 350px wide or more, .card gets display: flex, and the photo moves to the left of the title. The screen width has no effect at all. Only the wrapper's width matters.
Two steps: mark the container, then ask
- Mark the wrapper with
container-type. The official name for a container is containment context. - Write
@container (condition) { ... }: the question in the round brackets, then rules for the elements inside that wrapper.
You can't skip the first step. Without container-type, there is no container to ask. So the rules inside @container are never used, however wide the wrapper gets.
The rules inside @container style what is inside the container, not the container itself. That is why container-type goes on .wrapper, while .card is what changes.
The container-type values to know:
inline-size: the width only. This is the value you will use most.size: width and height. You have to give the container a height yourself, so this value is rarely used.normal: the default. The element's size cannot be asked about.
How it differs from a media query
A media query (@media) also asks about size. But it asks about the width of the viewport: the screen or the browser window. The Media Queries lesson covers it in full.
@media (min-width: 600px)checks the screen width.@container (min-width: 600px)checks the width of the nearest container.
Use @media for the layout of the whole page. Use @container for components you place in many spots, like cards or widgets.
⚠️ The card won't change, however wide the wrapper gets?
container-typeon the element you want to change. Putcontainer-typeon the wrapper, then write rules for its children. A container cannot change its own look through@container.- A wrapper whose width comes from its content. With
inline-size, the container's width is no longer calculated from what is inside it. If the wrapper isinline-blockor a flex item with no width, its width can shrink to 0. Give it a clear width, for examplewidth: 100%. - Nested containers. When one container is inside another container,
@containerwithout a name always asks the nearest container. To ask one further out, give that container a name:container-name: card;, then write@container card (min-width: 350px).
TL;DR: A container query changes the look of a component (for example a card) to fit the width of the box around it, not the width of the screen. Step 1: give the wrapper container-type: inline-size, so it becomes a container (a box whose width your CSS can ask about). Step 2: write @container (min-width: 350px) { ... } with rules for the elements inside the container. Those rules only apply when the container is at least 350px wide. Without container-type, the rules inside @container are never used. @media checks the viewport (the screen or the browser window), and @container checks the container.
⚡ Mark the wrapper
Write:
.wrapper {
container-type: inline-size;
}
Meaning: .wrapper is the box around a card. container-type: inline-size makes .wrapper a container: a box whose width your CSS can ask about. That lets the card change its look to fit the width of .wrapper, not the width of the screen. Without this step, the rules inside @container (the next step) are never used.
⚡ Then ask about its width
Write:
@container (min-width: 350px) {
.card {
display: flex;
gap: 16px;
}
}
Guess first: .card holds a photo and a title, and .wrapper wraps it. The screen doesn't change, only .wrapper grows wider than 350px. Does the card change?
Yes. @container asks about the width of .wrapper, not the width of the screen. .card gets display: flex, so the photo, which was above the title, moves to the left of the title.
Meaning: @container (min-width: 350px) is a question: is the nearest container (here .wrapper) at least 350px wide? If yes, the rule inside its curly braces applies. That rule styles what is inside the container, not the container itself. That is why container-type goes on .wrapper, while .card is what changes.
⚡ How it differs from a media query
Meaning: A media query (@media) also asks about size, but it asks about the width of the viewport: the screen or the browser window. @container asks about the width of the nearest container. Use @media for the layout of the whole page, and @container for components like a card, which you place in many spots.