CSS container queries: components that adapt to their space

Advanced  Nov 3, 2026

Style a component by the width of its container, not the screen.

CSS container queries: components that adapt to their space
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

The problem

A card in a wide main column and the same card in a narrow sidebar need different layouts. Media queries only know the screen width.

Define a container

.card-wrap {
  container-type: inline-size;
  container-name: card;
}
How the code in this section fits into the page.
How the code in this section fits into the page.

Query it

.card { display: grid; gap: 12px; }

@container card (min-width: 480px) {
  .card { grid-template-columns: 160px 1fr; align-items: start; }
}

The card now changes layout depending on the space it gets, wherever it is placed.

Container units

cqi is 1% of the container's inline size:

.card h3 { font-size: clamp(1rem, 4cqi, 1.75rem); }

Rules

  • The queried element must be a descendant of the container, not the container itself.
  • inline-size containment means the container's width cannot depend on its children.
  • Keep media queries for page-level layout and use container queries for components.
  • Browser support is good in current browsers. Write a sensible default layout first so older browsers still get a usable page.
The main points of this section.
The main points of this section.

More tutorials