Container Query

Responsive

Container Query

A container query is a rule that reshapes a layout based on the size of its parent container, not the whole screen, so a component adapts to wherever it sits.

Responsive cardThe layout adapts to the container width.
Container 300px
Container width300px

Definition

A container query decides how to lay something out based on the size of the parent element that holds it, not the size of the whole screen. The same card component can place its image and text side by side when it sits in a wide spot, then stack them top to bottom when it lands in a narrow one, reshaping itself on its own. Where a breakpoint looks at the size of the entire window to make its call, a container query looks at the width of the actual slot the element is standing in. You reach for it through the modern CSS @container rule when you want a component to adapt to the context it happens to be placed in.

Why does it matter?

Modern screens reuse a single component in many different spots. The same product card shows up in a wide article body, in a narrow sidebar, and inside a two-column grid. If your only yardstick is the screen width, a card dropped into a cramped sidebar will still insist on a horizontal layout just because the screen is wide, squishing itself into something awkward. A container query lets each card read the width of its own slot directly, so it appears in a shape that fits wherever it lands. Because of that, one well-built component can be reused in many places without being tweaked for each spot, which lifts the consistency and maintainability of your design system. Since the component takes responsibility for its own context, you can later change the layout or move it to a new spot without ever having to touch the card again.

Common mistakes

  • Forgetting to declare the parent as a container. An @container rule only works if the parent you want to target has a container-type set on it, and if you skip that declaration the rule is silently ignored and nothing changes at all.
  • Pushing container queries into every situation. Reshaping the big skeleton of the whole page is still a better fit for breakpoints. Container queries shine brightest on self-contained components that get reused from spot to spot.
  • Letting a child change the size of the very parent you designated as the container. If the container grows again in response to its child, that loop can make the layout jitter, so keep a clear split between the parent that sets the size and the child that reacts to it.

Practical tips

  • Reach for a container query whenever the right yardstick is the slot an element sits in rather than the whole screen. Since the card adapts to its own width in a sidebar or an article body alike, this fits especially well when you are building components meant to be reused anywhere.
  • Prepare a fallback with @supports for environments where support is not yet complete. If you make sure the component still shows a reasonable default, like a simple vertical stack, even without container queries, your content stays intact and safe in any browser.
  • Naming your containers lets you pick exactly which parent to measure against even when several are nested. Even in a complex structure where a card sits inside another card, the layout reacts to the container you intended and avoids surprise shifts.

Ask your AI

The same card squishes when it lands in a narrow sidebar

The component I'm pasting below switches its layout with screen-width media queries, so when it drops into a narrow sidebar on a wide screen it stubbornly stays horizontal and gets squished. Rewrite it to use the @container rule so it reacts to the width of the slot it actually sits in, not the whole screen. Set container-type: inline-size on the wrapping parent (skip this and the rule is silently ignored, so nothing changes at all), and stack the image and text vertically when the slot is under 400px, laying them side by side above that. Add an @supports fallback to a plain vertical stack for browsers that don't support container queries yet, and give me the updated code plus a short note on what you changed and why.

I added @container but the layout never changes

The code below uses an @container rule but the layout never changes. Diagnose why it's being silently ignored — first and foremost, check whether the parent you're trying to respond to is missing container-type: inline-size (without it, @container does nothing at all). If cards are nested inside other cards, use container-name to pin down exactly which parent to measure against, and also check for a loop where a child resizes the very container it's supposed to react to, which makes the layout jitter. Give me the root cause, the fixed code, and a short checklist to keep it from happening again.

Not sure whether to use a media query or a container query

In the layout I'm pasting below, sort each element into 'overall page skeleton' versus 'self-contained component that gets reused from spot to spot.' Leave the big page frame (the top-level grid, header, and footer placement) on screen-based breakpoints where they belong, and only move the reusable pieces — like a card that shows up in a sidebar, the article body, or a grid alike — to @container inside a parent that has container-type set. For each element, add a one-line rationale for 'breakpoint vs container query,' and for anything you'd move to a container query, suggest a slot-width threshold for it to react at.

Related concepts