StealThis .dev

Container Query Cards

A product card that reflows itself based on the width of its own container, not the viewport, using CSS container queries to swap between stacked, horizontal, and compact layouts inside three independently resizable panels you can drag.

Open in Lab
css
Targets: JS HTML

Code

Container Query Cards

The same card markup rendered three times, each inside a container of a different width. Instead of reacting to the browser viewport, every card measures its own parent using the CSS @container rule paired with container-type: inline-size. Below roughly 240px it collapses to a compact row with just a thumbnail and title, around 340px it becomes a stacked layout with the image on top, and past 480px it snaps to a horizontal editorial layout with the media beside the copy.

Each panel has a draggable resize handle so you can watch the breakpoints fire in real time. A live badge in the corner of every card reports the current container width and the active layout name, and an inline width readout updates as you drag. The point is that these components are genuinely context-independent: drop the exact same card into a sidebar, a modal, or a full-bleed hero and it adapts to the space it is given rather than to the size of the window.

The demo relies only on container-type, container-name, and @container queries, with a @supports fallback that keeps the cards perfectly usable (stacked layout) in engines that lack container query support. Drag interactions are pointer-based, keyboard accessible via the arrow keys on each handle, and the reported metrics are driven by a ResizeObserver so the labels stay in sync with whatever the CSS decides.