CSS Snippet
CSS Container Queries
Components that respond to their own container, not the viewport. The future of responsive design.
Last updated: October 2026
Quick answer: Set container-type: inline-size on a wrapper, then use @container (min-width: ...) to style descendants. Use container query units (cqi, cqw) for fluid scaling inside components. Use media queries for page layout and container queries for component layout.
🎬 Interactive Live Demo
Drag the slider to resize the container. The card inside responds to its own width — not the viewport.
💡 Drag the slider past 400px to see the card switch from vertical to horizontal — driven by container size, not viewport.
The Problem with Media Queries for Components
Media queries look at the viewport. But a component doesn't care about the viewport — it cares about the space it's given:
/* ❌ Media query — doesn't work when the card is in a sidebar */
.card { display: flex; flex-direction: column; }
@media (min-width: 1024px) {
.card { flex-direction: row; }
}
/* Problem: On a 1920px desktop, the card is in a 300px sidebar.
The viewport is wide, so the card goes horizontal.
But the card only has 300px — it looks broken. */
Container queries fix this. The card responds to its parent's width, not the viewport.
The Basic Syntax
/* 1. Define the container */
.card-wrapper {
container-type: inline-size;
container-name: card;
}
/* 2. Style descendants based on container size */
@container card (min-width: 400px) {
.card {
flex-direction: row;
}
}
Two properties set up the container:
container-type: inline-size— the container can be queried by its inline (horizontal) size.container-name: card— an optional name. If omitted, the nearest ancestor container is used.
Container Query Units
Container queries come with their own units, relative to the container — not the viewport:
@container card (min-width: 400px) {
.card-title {
font-size: clamp(1rem, 0.5rem + 3cqi, 2rem);
}
}
The three units:
cqi— 1% of the container's inline size (usually width).cqw— 1% of the container's width.cqb— 1% of the container's block size (usually height).
Combined with clamp(), these create fluid typography that scales with the component, not the screen.
Container Queries + clamp() = Fluid Components
This combination is the modern way to build self-contained, responsive components:
.card-wrapper {
container-type: inline-size;
}
.card-title {
font-size: clamp(1rem, 0.5rem + 3cqi, 2rem);
}
.card-body {
padding: clamp(1rem, 5cqi, 2rem);
}
The title and padding scale with the card's container. Move the card from a full-width page to a narrow sidebar, and everything adjusts automatically.
Media Queries vs Container Queries — When to Use Each
| Use Case | Tool |
|---|---|
| Page grid columns | @media |
| Sidebar visibility | @media |
| Card layout (row vs column) | @container |
| Component font sizing | @container + cqi |
| User preferences (dark mode) | @media |
Use media queries for macro layout (page structure). Use container queries for micro layout (components).
🎯 Container Queries Cheatsheet
| Goal | Code |
|---|---|
| Define container | container-type: inline-size |
| Name a container | container-name: card |
| Query container width | @container card (min-width: 400px) |
| Fluid text in container | font-size: clamp(1rem, 3cqi, 2rem) |
| Query nearest container | @container (min-width: 400px) |
⚠️ Common Mistakes
- Forgetting to set
container-typeon the parent. The@containerrule won't match anything without it. - Trying to style the container itself. Container queries only style descendants of the container, not the container element itself.
- Setting
container-type: sizewithout a height. This queries both dimensions, but the container will collapse if it has no explicit height. Useinline-sizeunless you specifically need both dimensions. - Using viewport units inside container queries.
vwstill refers to the viewport. Usecqiorcqwfor container-relative sizing. - Mixing up
container-nameandcontainer-type.nameis for targeting.typeis required for the container to be queryable at all. - Expecting
@containerto replace all media queries. Use both. Media queries for the page, container queries for the components.
❓ Frequently Asked Questions
What are CSS container queries?
Container queries let you style elements based on the size of their parent container, not the viewport. A card component can respond to the space it's given in a sidebar, a grid cell, or a modal — without needing to know the viewport width.
What is the difference between media queries and container queries?
Media queries look outward at the viewport. Container queries look inward at the component's own parent container. Use media queries for page-level layout and container queries for component-level layout.
What is container-type in CSS?
container-type defines what dimensions of an element can be queried. Set container-type: inline-size to query the inline (horizontal) size. Set container-type: size to query both dimensions, but this requires an explicit height.
What are container query units (cqi, cqw, cqb)?
Container query units are like viewport units but relative to the container. 1cqi = 1% of the container's inline size. 1cqw = 1% of the container's width. 1cqb = 1% of the container's block size. They let components scale fluidly within their own context.
Are container queries supported in all browsers?
Yes. Container queries have 94% browser support and are available in all modern browsers including Chrome, Safari, Firefox, and Edge. Safari shipped support in 2022, and Chrome and Firefox followed in 2023.