Sub topic 11
Container queries for reusable components
This chapter assumes you can write media queries, use Grid or Flexbox, and know the cascade. It covers how a component can respond to the space it is placed in, not to the screen.
The problem with viewport queries
A media query looks at the viewport. A card placed in a narrow sidebar on a wide desktop screen still sees a "wide" viewport, so a rule like @media (min-width: 48em) can give it a layout that does not fit.
You can patch this with extra selectors such as .sidebar .card. That ties the component to its location and the patches pile up. Container queries fix the cause: the component asks how wide its container is.
Declaring a container
A container query has two parts. First, mark an ancestor as a query container. Second, write an @container rule that targets its descendants.
.card-list {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 40rem) {
.card {
display: flex;
gap: 1rem;
}
}container-type: inline-size lets descendants query the container's inline size, which is its width in horizontal writing modes. The base .card rule is the small layout. The @container rule adds the horizontal layout only when .card-list is at least 40rem wide. This is the same mobile-first pattern as before: base styles first, then min-width additions.
Rules to remember
- A container cannot style itself with its own query. Only descendants are affected, so put the changing styles on a child, not on the element with
container-type. inline-sizecontainment means the container's width cannot depend on its contents. Give it a width from the layout, such as a grid track or a block element that fills its parent.- Queries match the nearest ancestor that is a container. If you have several, name them.
Naming containers
Use container-name when a component is nested inside other containers, or when you want to be explicit about which one to query.
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (min-width: 20rem) {
.widget {
padding: 1.5rem;
}
}The shorthand container: sidebar / inline-size; sets both properties at once.
Media queries or container queries?
| Question | Use |
|---|---|
| Does the page structure change, such as the number of columns in the main layout? | Media query |
| Does a reusable component change depending on where it sits? | Container query |
| Does the user's device or settings matter, such as hover, pointer or reduced motion? | Media query |
Most stylesheets use both. Media queries arrange the page. Container queries let each component adapt inside the space it receives.
Common errors
- Nothing happens. The ancestor is missing
container-type, or you applied the changing styles to the container itself. - The container collapses to zero width. With
inline-sizecontainment, an element that relied on its content for width, such as afloator an inline-block, no longer gets that width. - Wrong container is matched. The nearest container ancestor wins. Add a
container-nameand query it by name. - Units confusion. Inside an
@containerrule,cqwunits are relative to the container's width. Use them only after confirming which container applies.
Exercise
A .promo component sits in both a wide main column and a narrow sidebar. You wrap each in <div class="slot">, and you want .promo to switch to a two-column layout when its slot is at least 30rem wide. Which CSS do you write, and why can't you put the layout change on .slot itself?
Show answer
Write .slot { container-type: inline-size; } and @container (min-width: 30rem) { .promo { display: grid; grid-template-columns: 1fr 1fr; } }. A container cannot be styled by its own container query. Only its descendants can, so the layout change goes on .promo, a child of .slot.