WHAT YOU NEED TO KNOW
CSS container queries allow you to style elements based on the size of their parent container rather than the entire browser viewport, making true component-driven design possible.
- Modern browser support for size container queries reached 100% baseline compatibility across all major rendering engines as of 2024.
- You define a container using the
container-typeproperty, setting it to eitherinline-size(width) orsize(width and height) to establish a containment context. - Special units like
cqwandcqhscale elements relative to 1% of the container’s width or height, allowing fine-grained component scaling.
However, a container element cannot style itself based on its own query: you must always target descendant elements within the containment context.
Why Are CSS Container Queries Essential for Responsive Design?
For years, responsive design relied entirely on CSS media queries, which evaluate the width of the entire viewport. If you wanted to build a reusable card component that worked perfectly in both a wide hero section and a narrow sidebar, media queries forced you to write complex, context-specific overrides. In contrast, css container queries let the component adapt directly to the dimensions of its immediate parent wrapper.
According to documentation on MDN Web Docs, this shift moves us from layout-driven responsive design to true component-driven design. This architectural improvement means a single component can contain all its layout logic, making codebases far more modular. You can drop a component anywhere on your page, and it will self-adjust according to the space it is given.
Defining a Containment Context
To use container queries, you must first register an element as a containment context. This tells the browser engine to monitor this element’s dimensions and style changes.
The container-type Property
The container-type property dictates what kind of containment context you want to establish. Setting this to inline-size instructs the browser to watch the width of the parent container, which is the standard choice for horizontal layouts. Setting it to size monitors both width and height, though this requires you to explicitly define the height of the container to prevent circular layout loops.
The container-name Property
The container-name property applies a unique identifier to your container. This becomes highly useful when you have nested containers and want to target a specific parent rather than the closest ancestor. You can assign any custom identifier as the name, provided it does not conflict with reserved CSS terms like default or none.
Shorthand Container Syntax
You can combine both properties into a single shorthand declaration using the container property. The format uses a forward slash to separate the name and the type. For example, writing container: sidebar / inline-size; accomplishes both registration tasks in one clean line of code.

Querying the Container
Once you establish a containment context, you write a conditional rule using the @container at-rule. This is the core mechanism of any @container css tutorial or design workflow.
Here is how the query evaluation process operates:
- The browser searches upward through the DOM tree from the target element to find the nearest ancestor with an established containment context.
- If the query specifies a name, the browser skips any unnamed intermediate containers and targets the matching named ancestor.
- The browser evaluates the conditions inside the parentheses, such as
(width > 400px), using standard range operators. - If the conditions are met, the browser applies the nested CSS rules to the target descendant elements.
For a clear code example, you would write @container (width > 500px) { .card-title { font-size: 1.5rem; } } to style your heading.
Working with Named Containers
Named containers prevent style conflicts when building complex layouts with multiple nested container queries responsive design elements. If you do not name your containers, a component will always query its immediate parent container, which might not be the layout container you intended to target.
Consider a product grid inside a sidebar. You can define the sidebar as container: sidebar / inline-size; and the grid item as container: card / inline-size;. In your CSS, writing @container sidebar (width > 300px) ensures your styles apply specifically when the sidebar itself expands, ignoring any intermediate card sizes.
CSS Container Query Units
Size container queries introduce new relative length units that are calculated based on the dimensions of the nearest containment context. If no container is available, these units default to the small viewport units (svw or svh).
The following table outlines the six primary container query units:
| Unit | Description | Viewport Equivalent |
|---|---|---|
| cqw | 1% of the query container’s width | svw |
| cqh | 1% of the query container’s height | svh |
| cqi | 1% of the query container’s inline size (width in horizontal mode) | svi |
| cqb | 1% of the query container’s block size (height in horizontal mode) | svb |
| cqmin | The smaller value between cqi and cqb | svmin |
| cqmax | The larger value between cqi and cqb | svmax |
Using font-size: 4cqi; allows your text to scale perfectly relative to its parent container width, keeping typography balanced across all component variations.

Container Style Queries
Beyond size queries, modern CSS allows you to query the active styles applied to a parent element. This feature, known as style queries, is useful when you want to change child styles based on a custom property value set on the parent.
As highlighted in style guides on CSS-Tricks, style queries do not require setting a container-type of size or inline-size because style containment is enabled by default. You can query custom properties directly using syntax like @container style(--theme: dark) to adapt child background and text colors. This allows for highly flexible theme switching without relying on heavy JavaScript manipulation.
Nesting Container Queries
You can nest container queries inside one another to create highly sophisticated, multi-layered responsive components. When you nest queries, the inner query will evaluate against its closest containment context, while the outer query continues to evaluate against its respective parent.
For example, you might have an outer container query checking the inline-size of a dashboard widget wrapper. Inside that rule, you can write another container query targeting a smaller card component layout. This nesting pattern guarantees that the typography scales accurately relative to the card while the card’s column span adapts to the overall dashboard grid.
Browser Support and Fallbacks
As of 2026, browser support for CSS size container queries is virtually universal, meaning you can confidently use them in production without major compatibility concerns. However, if you are supporting legacy environments, implementing a robust fallback strategy remains a recommended practice.
Here are the primary ways to handle container query fallbacks:
- Use standard CSS Grid and Flexbox layouts to create fluid, wrap-around component layouts that naturally adapt without rigid media queries.
- Utilize the
@supportsrule to detect container query support, writing@supports (container-type: inline-size)to separate your query layouts from legacy styling. - Provide a default mobile-first layout utilizing single-column structures that display correctly regardless of browser parsing capabilities.
- Leverage reliable polyfills in your build steps if your target audience uses outdated enterprise browsers.
Testing your components across different browser builds ensures your layouts remain functional and visually appealing, even on older devices.
