CSS container queries: components that adapt to their own space
Container queries let a component change its layout based on the size of its parent instead of the viewport. Learn the syntax, container units and when to use them.
Media queries ask a question about the whole viewport: how wide is the window? That works for page layout, but it breaks down for reusable components. A card that sits in a wide main column on one page and in a narrow sidebar on another has the same viewport in both places and needs two different layouts.
Container queries fix this. They let a component respond to the size of the element that contains it.
The basic syntax
First, mark an element as a container. Then write an @container rule that applies to its descendants.
.card-wrapper {
container-type: inline-size;
container-name: card;
}
.card {
display: grid;
gap: 1rem;
}
@container card (min-width: 480px) {
.card {
grid-template-columns: 160px 1fr;
}
}With container-type: inline-size, the browser watches the container's width. When it reaches 480 pixels, the card switches to two columns. The same card in a narrow sidebar stays stacked, with no change to the component's code.
You can write the declaration in one line with the shorthand:
.card-wrapper {
container: card / inline-size;
}The container cannot style itself
A query applies to the descendants of a container, never to the container itself. That is why the example uses a wrapper element and a separate .card. If a query seems to do nothing, check that the element you are styling is inside the container and not the container.
Container units
Inside a container you can size things relative to it with container query units: cqw is 1% of the container's width, and cqh is 1% of its height. They work like viewport units, but follow the component.
.card h2 {
font-size: clamp(1.1rem, 4cqw, 1.75rem);
}Media queries or container queries?
Use both, for different jobs:
| Question | Use |
|---|---|
| How should the page shell be arranged? | Media query |
| Is the user on a dark theme, or prefers reduced motion? | Media query |
| How should this component look in the space it was given? | Container query |
A good rule is to let media queries shape the page and container queries shape the components inside it.
Support
Size container queries are available in all current major browsers and have been part of Baseline since 2023. Check the MDN page for the latest compatibility data, in particular for newer additions such as style queries, which are not supported as widely.
Pitfalls
container-type: inline-sizeapplies containment, so the container cannot depend on its children's width to size itself- Avoid making every element a container, and mark only the wrappers of components that need to adapt
- Give containers a name when you nest them, so a query targets the one you mean
Takeaway
Put container-type: inline-size on the wrapper, write @container rules for the component, and let it adapt wherever it lands. Your components stop depending on where they are used.
Comments
Comments are optional. Loading them connects your browser to Giscus and GitHub. Read the privacy details.
Get new notes by email
Keep reading
WebTechnologies
Configuring Tailwind CSS v4 theme tokens in your CSS
WebTechnologies
How I built this blog, the logic behind it, and how to build yours
WebTechnologies
MDX in practice: components for a technical blog