Skip to content
IndexRequest a UIAboutContact

Container Query Cards

A product card that switches between stacked, side-by-side and compact layouts based on its own container width.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
UI component sheet showing the same product card in three container widths side by side: a wide version with the image on the left and text on the right, a medium stacked version with the image on top, and a narrow compact version with a small image and only the title and price, each inside a dashed outline labelled with its width, clean light theme, flat style, consistent spacing. Negative: gradients, heavy shadows, text errors, device frames. Reduced motion: static sheet.

About this pattern

Definition

A container query lets a component respond to the width of its parent instead of the whole screen. The same card can sit in a wide main column, a narrow sidebar or a modal and always pick the layout that fits, without knowing where it is placed.

Purpose & UX principles

  • Mark the parent with container-type: inline-size.
  • Write the card's rules inside @container (min-width: 260px) { ... }.
  • Media queries still handle page-level changes; container queries handle components.
  • Keep two or three layouts per component, not ten.

Interaction states

  • A Container width slider from 150 to 316 pixels and a live layout label.

Motion specification

  • The card reflows instantly as the slider moves.
  • Reduced motion: nothing animates.

Responsive behaviour

  • Give the container a sensible default so the card works if queries are unsupported.
  • Test the component in a narrow slot, not only at full width.

Code

HTML
<div class="bx"><div class="hold" id="hold"><article class="cd"><i class="ph"></i><div class="tx"><b>Studio lamp</b><span>Warm light in three colours.</span><u>$48</u></div></article></div><label class="rw">Container width <b id="wv">316px</b><input id="wr" type="range" min="150" max="316" value="316"></label><p class="out" id="out"></p></div>

Related patterns

Responsive Breakpoint Preview

↗
Responsive Breakpoint PreviewLayout & Scrolling

Masonry Columns Layout

↗
Masonry Columns LayoutLayout & Scrolling

Interactive Card

↗
Interactive CardCards & Containers