Introduction
For over fifteen years, responsive web design relied entirely on viewport media queries (@media (min-width: 768px)).
However, viewport queries have a fundamental flaw: they evaluate the width of the entire browser window, not the width of the component's parent container.
A product card placed in a narrow sidebar needs a stacked mobile layout, even on a 4K desktop monitor.
CSS Container Queries (@container) solve this by allowing components to respond directly to the available width of their immediate parent container.

Declaring a Container Context with container-type
To establish a container query boundary, declare container-type: inline-size on the parent wrapper element.
This instructs the browser layout engine to track the inline (horizontal) width of this specific container.
You can optionally name the container using container-name: card-wrapper for targeted scoping.
All descendant child elements can now query this container boundary.
Writing Container Query Rules with @container
Inside your component stylesheet, write responsive rules based on container dimensions:
@container (min-width: 450px) { .card { display: flex; flex-direction: row; } }.
If the card is dropped into a full-width section, it automatically renders horizontally with image on the left and text on the right.
If dropped into a narrow 1/3 column or sidebar, it automatically stacks vertically with image on top.
The component is 100% self-contained and universally reusable across any layout context.
Container Query Units: cqw and cqh
Container queries introduce dedicated relative units: 1cqw equals 1% of the query container's width.
You can size typography, margins, and padding dynamically relative to the component container rather than the viewport.
Heading text scales proportionally whether the component is displayed inside a modal, sidebar, or hero grid.
Container queries are fully supported across all modern evergreen browsers.
Frequently Asked Questions
Are container queries supported in all modern browsers?: Yes, container queries have been supported across Chrome, Safari, Firefox, and Edge since 2023.
Do container queries replace viewport media queries?: No. Use viewport queries for global page layouts (grid templates); use container queries for reusable component elements.
Do container queries cause performance lag?: Container queries are heavily optimized in browser C++ engines; container-type: inline-size isolates layout recalculations efficiently.