Frontend

CSS container queries: components that know their own size

Isometric stacks of blocks with a few highlighted in cyan

A card in a wide main column and the same card in a narrow sidebar should not look identical. For years we faked this with modifier classes. Container queries make it a property of the component itself.

.card-list { container-type: inline-size; }

.card { display: grid; gap: 1rem; }

@container (min-width: 36rem) {
  .card { grid-template-columns: 12rem 1fr; }
}

Where they shine

  • Reusable cards, media objects and teasers
  • Dashboards with resizable panels
  • Design-system components that ship without layout assumptions
Three glowing signal waves crossing a dark background

Pair them with clamp() and container units like cqi for type that scales with its box, not the window. Your components become portable in a way media queries never allowed.

Remember that an element cannot query itself: the container is always an ancestor.