Container queries in practice

A component that adapts to its own width works in a sidebar, a grid and a full row without extra variants.

Media queries answer “how wide is the screen?”. A card placed in a narrow sidebar on a wide screen needs a different question: “how wide is my slot?”.

Declare the container

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

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

The rules inside @container apply to descendants of the container, never to the container itself. That is why the slot wraps the card.

Size with container units

cqi is 1% of the container’s inline size. Combined with clamp() it scales a title with its card instead of with the viewport: font-size: clamp(1.1rem, 4cqi, 1.6rem). Keep the maximum under 2.5 times the minimum so browser zoom still works.