CSSApr 15, 2026 · Updated Oct 13, 2026 · 4 min read

CSS Container Queries: The Complete Guide (2026)

Container queries are the most significant CSS layout feature since Grid. While media queries respond to the viewport (browser window) size, container queries respond to a specific parent element's size. This means a card component can have different layouts based on where it is placed — sidebar, main content area, or full-width section — without any JavaScript or viewport-based hacks.

📦
Try the Container Query Builder
Free, no signup
→
DG
Derek Giordano
Designer & Developer
In this guide
01Container Queries vs Media Queries02Setting Up Container Elements03Writing @container Rules04Worked Example: Three Gotchas, Measured05Practical Component Patterns

Container Queries vs Media Queries

Media queries ask: "How wide is the browser window?" Container queries ask: "How wide is my parent element?" This distinction changes everything about component design.

With media queries, a card component breaks at 768px viewport width. But what if that card is in a 300px sidebar on a 1200px screen? The viewport is wide but the card space is narrow. Media queries cannot solve this — container queries can.

Container queries make components truly reusable. Drop a component anywhere on any page, and it adapts to the space it is given. No viewport assumptions, no parent-specific overrides, no JavaScript measurement loops.

Setting Up Container Elements

To use container queries, designate a parent element as a container with two properties: container-type and container-name. container-type: inline-size enables width-based queries (the most common). container-type: size enables both width and height queries.

container-name gives the container a label you reference in your @container rules. While optional, naming containers avoids ambiguity when you have nested containers.

The catch: container-type: inline-size applies size containment on the inline axis, so the container's width can no longer depend on its own content. That is what lets the browser size the container before styling its children — and it means a container that would normally shrink to fit its content (an inline-block, a float, an auto-width flex item) collapses to zero width. Give such containers an explicit width or a flex basis, or put container-type on a wrapper that already has a definite width.

Writing @container Rules

The syntax mirrors media queries: @container card-container (min-width: 400px) { ... }. Inside the block, write CSS that applies when the named container meets the condition.

You can use min-width, max-width, and logical combinations with and/or. Width is by far the most useful dimension — most responsive adjustments are about horizontal space.

Common breakpoint pattern: a card stacks vertically below 300px, goes side-by-side at 300-600px, and adds a third column above 600px. All based on the container width, not the viewport.

Worked Example: Three Gotchas, Measured

We ran a minimal test page in Chromium 141 (script and results in our test folder, October 13, 2026):

CaseResult
Inline-block with container-type: inline-size and text inside0 px wide
Auto-width flex item as a container0 px wide
400 px block container, child styled by @container (min-width: 300px)Rule applied
The container itself targeted by the same ruleNot applied — a container query styles descendants, never the container
font-size: 5cqi inside the 400 px container20 px (5% of the container's inline size)

Support is no longer a concern for size queries: MDN's compatibility data lists @container in Chrome and Edge 105, Safari 16 and Firefox 110, all shipped between September 2022 and February 2023. Style queries for custom properties arrived later (Chrome 111, Safari 18, Firefox 151 in May 2026), and scroll-state queries are still Chromium-only.

Practical Component Patterns

Article card: at narrow widths, stack image on top with title below. At medium widths, place image and text side by side. At wide widths, add a metadata sidebar. One component, three layouts, zero viewport assumptions.

Navigation: in a narrow sidebar, show icon-only navigation. In a wider sidebar, show icons with labels. In a full-width header, show a horizontal navigation bar. All driven by the container the nav is placed in.

Data table: at narrow container widths, switch from table layout to a stacked card layout where each row becomes a card with labeled fields. This is cleaner than horizontal scrolling.

Frequently Asked Questions

What browsers support container queries?+
Chrome 105+, Firefox 110+, Safari 16+, Edge 105+. All modern browsers support them. Production-ready for most projects as of 2024.
Can I nest containers?+
Yes. A container query matches the nearest ancestor container. Use container-name to target specific containers when nesting.
What is the performance impact?+
Minimal. Container queries use layout containment which can actually improve performance by limiting the scope of layout recalculations.
Do I still need media queries?+
Yes, for viewport-level layout decisions (page structure, column counts). Container queries handle component-level responsiveness within that structure.
Try it yourself

Build container queries visually — free, no signup.

⚡ Open Container Query Builder
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.