Snippets /

Container query cards

Grid that adapts to its container width, not the viewport.

@container (min-width: …)

🎨

Design system

Tokens and components.

Performance

Core Web Vitals.

🔬

Experimental CSS

Anchor positioning.

Widely supported
layout

Quick implementation

/* 1. Declare the query container on the wrapper */
.card-wrapper {
  container-type: inline-size;
  container-name: cards;
}

/* 2. Default: single column */
.card-grid {
  display: grid;
  gap: 1rem;
}

/* 3. Two columns when wrapper is ≥ 28rem */
@container cards (min-width: 28rem) {
  .card-grid { grid-template-columns: repeat(2, 1fr); }
}

/* 4. Three columns when wrapper is ≥ 44rem */
@container cards (min-width: 44rem) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}

/* 5. Compact (horizontal) card layout when narrow */
@container cards (max-width: 27.9rem) {
  .card { display: flex; }
  .card .thumb { width: 4.5rem; aspect-ratio: 1; flex-shrink: 0; }
}

Prompt this to your LLM

Includes compact card variant and a React component variant.

You are a senior frontend engineer.

Goal: Build a card grid that responds to container width (not viewport) using CSS container queries.

Technical constraints:
- Declare container-type: inline-size on the wrapper. Give it a container-name.
- Default: single column. At ≥28rem: 2 columns. At ≥44rem: 3 columns.
- Bonus: At <28rem, flip the card to a horizontal (flex) layout with a small square thumbnail on the left.
- Each card has: a thumbnail area (aspect-ratio: 16/9 or 1:1 in compact), a title, and a short description.

Framework variant (choose one):
A) Vanilla HTML + CSS — a self-contained demo.
B) React — a CardGrid component that accepts cards as an array of { title, description, emoji } props. The container-query logic lives in CSS; the component just renders a wrapper + children.

Edge cases:
- The grid should fall back to a single column if @container is unsupported (the default CSS already handles this).
- Don't use media queries — the whole point is container-aware layout.

Return clearly separated HTML + CSS (or TSX + CSS module).

Why this matters in 2026

Media queries respond to the viewport. But a card component might live inside a sidebar (narrow), a main column (medium), or a hero section (wide). If its layout depends on the viewport, the same component needs different breakpoints every time it's placed somewhere new. Container queries break that dependency: the component decides its layout based on the space it's given.

This is what makes components truly reusable. The compact-card variant (horizontal layout for narrow containers) is something you used to add with a JS ResizeObserver or a CSS hack — container queries make it one @container rule.

The logic

container-type: inline-size creates an "inline-size query container" for the wrapper. Children can then use @container name (min-width: X). container-name lets you target a specific ancestor when containers are nested.

The compact variant uses a max-width query: @container cards (max-width: 27.9rem) switches the card to display: flex so the thumb and text sit side by side — useful for narrow sidebars.

Accessibility & performance

Container queries are purely layout — they don't affect semantics. Use them to improve readability at narrow widths (e.g. larger touch targets, readable line lengths). The browser resolves container queries before painting, so there's no flash of wrong layout.