What Are CSS Container Queries and How Do They Work?
CSS container queries allow developers to apply styles to an element based on the size or styling of its direct parent container rather than the overall browser viewport. While traditional viewport media queries respond strictly to the dimensions of the entire browser window, container queries enable true component-driven responsive design. This distinction allows the same modular component to adapt dynamically whether it sits in a narrow sidebar, a wide main content area, or a multi-column grid layout.
The Limitation of Viewport Media Queries
For over a decade, responsive web design relied almost entirely on
CSS media queries (@media). Viewport media queries inspect
global page conditions such as screen width, screen height, orientation,
and resolution.
While effective for macro-layouts—such as switching a desktop multi-column page into a single-column layout on mobile devices—viewport queries struggle with modern component architecture. In design systems built with reusable elements (like cards, widgets, or navigation blocks), a component does not know where it will be placed on a page. A product card located in a narrow sidebar requires a stacked layout, even on a 4K desktop monitor. Conversely, that same card needs an expanded horizontal layout when placed in the main feed. Because viewport media queries only observe the global screen width, achieving this contextual styling historically required writing redundant CSS utility classes or relying on JavaScript resize observers.
How CSS Container Queries Work
CSS container queries solve this problem by establishing a local
styling context. An ancestor element is explicitly declared as a
container, allowing any child elements inside it to query its dimensions
using the @container rule.
To enable a container query, two core properties are typically defined on the parent wrapper:
container-type: Specifies the axis to track. Usinginline-sizetracks the container's width (the inline direction in horizontal writing modes), whilesizemonitors both width and height.container-name(optional): Gives the container a custom identifier so nested components can query a specific ancestor rather than the nearest one.
The shorthand property
container: <name> / <type>; can also be used to
define both values at once.
Basic Syntax Example
To set up a container and adjust a child card component based on its wrapper width:
/* 1. Define the container context on the parent */
.card-wrapper {
container-type: inline-size;
container-name: card-container;
}
/* 2. Default styles for the component */
.card {
display: flex;
flex-direction: column;
gap: 1rem;
}
/* 3. Apply styles when the parent container reaches 450px or wider */
@container card-container (min-width: 450px) {
.card {
flex-direction: row;
align-items: center;
}
}Key Differences: Container Queries vs. Media Queries
The functional differences between these two CSS features impact how frontend code is structured and scaled across modern web applications.
| Feature | Viewport Media Queries (@media) |
Container Queries (@container) |
|---|---|---|
| Trigger Source | Browser viewport dimensions and system preferences | Parent element's computed dimensions or styles |
| Primary Use Case | Macro page layouts, global breakpoints, dark mode detection | Modular UI components, design systems, isolated widgets |
| Component Portability | Low; components depend on total screen size | High; components adapt anywhere in the DOM |
| New CSS Units | Viewport units (vw, vh, vmin,
vmax) |
Container query units (cqw, cqh,
cqi, cqb) |
Container Query Length Units
Alongside the @container rule, CSS introduced dedicated
container query units. These units allow typography, margins, and
padding to scale relative to the container's size rather than the
browser window:
cqw: 1% of the query container’s widthcqh: 1% of the query container’s heightcqi: 1% of the query container’s inline sizecqb: 1% of the query container’s block sizecqmin: The smaller value betweencqiandcqbcqmax: The larger value betweencqiandcqb
For example, setting font-size: clamp(1rem, 4cqi, 2rem);
ensures that text scales smoothly within the component itself,
preventing oversized headings when the component is placed inside
constrained spaces.
When to Use Each Approach
Container queries do not replace media queries; rather, they complement them.
Use viewport media queries for global layout scaffolding, root-level
grid adjustments, print styles, and user preference checks such as
@media (prefers-color-scheme: dark) or
@media (prefers-reduced-motion).
Use container queries for self-contained UI modules—such as cards, comment threads, data tables, and search bars—ensuring they render correctly regardless of where they are placed in a design layout.