Container Queries
How @container lets a component respond to the space it has rather than the screen size, what container-type does, and how container query units work.
Container Queries
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A container query asks about the size of an ancestor, not the viewport. This lets the same card adapt in a wide main column and in a narrow sidebar without knowing which page it is on.
- You opt an element in with `container-type: inline-size` (and optionally `container-name`), then style its descendants inside `@container (width > 400px) { ... }`. The shorthand is `container: card / inline-size`.
- A container can never be styled by its own query, only its descendants can. A rule for the container inside its own `@container` has no effect.
- `container-type: inline-size` applies size containment on the inline axis, so the container's width can't come from its content. An `inline-block` container with this set measured 0px wide in a test.
- Container query units size things relative to the container: `1cqi` is 1% of its inline size. Outside any container they fall back to viewport units.
Components that don’t know where they are
Media queries answer “how wide is the screen?”. That is the wrong question for a component. A product card might sit in a three-column grid, a narrow sidebar or a full-width hero, all on the same screen. What it needs to know is how much space it has.
Before container queries, the usual workaround was extra classes (.card--compact) set by the parent, or JavaScript measuring widths. Container queries make this a CSS question. Think of a picture frame that rearranges what is inside it based on its own size, whichever wall it hangs on.
Setting up a container
Two steps. Mark an element as a container, then query it:
.card-wrap { container-type: inline-size; container-name: card; }
.card { display: grid; gap: 1rem; }
@container card (width >= 28rem) {
.card { grid-template-columns: 8rem 1fr; }
}
container-type: inline-size means “track my width”, so queries about width work. The container-name is optional, but a name lets you target a specific ancestor instead of the nearest one. The shorthand is container: card / inline-size. Inside @container, the same range syntax from media queries works.
In a test, a .card of width: 100px inside a 200px container stayed 100px, and inside a 400px container, with a rule that sets width: 250px above 300px, became 250px. The same CSS, two different outcomes, decided by the parent’s width.
Two rules to remember
A container can only style its descendants. A query about .wrap can’t change .wrap, because that would be circular: the size is the thing being measured. A test with @container (width > 300px) { .wrap { height: 80px } } left the container at its original 20px height. Put the container on a wrapper and style the children.
Containment has a cost. For the browser to measure the container without circularity, inline-size containment promises that the container’s width doesn’t depend on its content. A normal block fills its parent anyway, so this is fine. But a shrink-to-fit element, like display: inline-block or a float, has nothing to size from. In a test, an inline-block container measured 0px wide. Give it a width or use block layout. Also, container-type: size contains both axes and needs an explicit height as well, so inline-size is the right default.
Container query units
You can also size things relative to the container. The units are cqw and cqh (1% of the width or height), cqi and cqb (inline and block axis), and cqmin and cqmax:
.card-title { font-size: clamp(1rem, 4cqi, 2rem); } /* text scales with the card */
In a test, width: 50cqi inside a 400px container measured 200px. With no container ancestor, it fell back to the small viewport size: 400px at an 800px viewport. This works well with clamp() for type that scales with its component.
Style queries, briefly
@container style(--theme: dark) { ... } queries a custom property’s value on a container instead of its size. That is useful for theming components, since no container-type is needed. Style queries for custom properties became available in all major browsers in May 2026, so they are new. Check that your audience’s browsers are covered before relying on them.
Media or container?
Use a media query for page-level decisions: the overall layout, navigation, anything about the screen or the user. Use a container query for component-level decisions: how a card, a table or a form arranges itself. Container queries are Baseline, available in every major browser since February 2023, so reaching for them is no longer a risk.
What the video covers
The embed is a clip, 12:07 to 28:58, from Kevin Powell’s “Learn how to use Media queries & Container queries” (January 2024). The chapters in that range are container query basics, the difference between media and container queries, naming our containers, the container shorthand, a quick recap, container query units, and whether to define the html element as a container. Style queries (28:58) and the “which one should you be using?” wrap-up (32:50) are outside the clip, so the full video may be worth finishing. I could not read a transcript, so I do not describe the demos. The containment cost and the self-styling rule are the prose’s own and were checked in a browser. The runner-up covers patterns that rely on a container’s font size.
Where this fits
This takes the idea from media queries and mobile-first down to the component level. It pairs with subgrid for cards that adapt and align. The next lesson, viewport units and dynamic viewports, covers the units that do track the screen.
Check yourself
A card has a media query that switches it to a horizontal layout at 600px viewport width. It looks wrong in a 300px sidebar on a wide screen. Why, and what is the fix?
The media query looks at the viewport, which is wide, so it applies the horizontal layout even in a narrow sidebar. Wrap the card in an element with `container-type: inline-size` and replace the media query with `@container (width > 400px)`, so it responds to the space it actually has.
You write `.wrapper { container-type: inline-size }` and then `@container (width > 300px) { .wrapper { padding: 2rem } }`. Nothing happens. Why?
A container query can only style descendants of the container, never the container itself. Put the padding on a child element, or add a wrapper around the content.
After adding `container-type: inline-size` to an inline-block element, it collapses to zero width. Why?
`inline-size` containment means the element's width can't depend on its content, so a shrink-to-fit element has nothing to size from. Give it a width, or use a block or grid/flex item that gets its width from its parent.
What does `width: 50cqi` do on a child of a container that is 400px wide?
It is 50% of the container's inline size, so 200px. If no ancestor is a container, `cqi` falls back to the small viewport's equivalent, so in an 800px viewport it was 400px.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.