Container Query Cards
A product card that switches between stacked, side-by-side and compact layouts based on its own container width.
AI prompt
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
<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>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.hold{container-type:inline-size;width:var(--w);max-width:100%;border:1px dashed #b9b9b4;border-radius:12px;padding:8px;background:var(--surface)}.cd{display:grid;gap:8px}.ph{display:block;height:54px;border-radius:8px;background:linear-gradient(135deg,#f6d7b8,#e9b98e)}.tx{display:grid;gap:2px;font-size:12px}.tx b{font-size:13.5px}.tx span{color:var(--muted)}.tx u{text-decoration:none;font-weight:700}
@container (min-width:240px){.cd{grid-template-columns:96px 1fr;align-items:center}.ph{height:76px}}@container (max-width:150px){.tx span{display:none}.ph{height:34px}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const hold=document.getElementById('hold'),wr=document.getElementById('wr');
function up(){hold.style.setProperty('--w',wr.value+'px');const w=hold.clientWidth-18;document.getElementById('wv').textContent=wr.value+'px';document.getElementById('out').textContent='Content width '+w+'px: '+(w>=240?'side-by-side layout.':w<=150?'compact layout, description hidden.':'stacked layout.');}
wr.addEventListener('input',up);up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Container Query Cards — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.hold{container-type:inline-size;width:var(--w);max-width:100%;border:1px dashed #b9b9b4;border-radius:12px;padding:8px;background:var(--surface)}.cd{display:grid;gap:8px}.ph{display:block;height:54px;border-radius:8px;background:linear-gradient(135deg,#f6d7b8,#e9b98e)}.tx{display:grid;gap:2px;font-size:12px}.tx b{font-size:13.5px}.tx span{color:var(--muted)}.tx u{text-decoration:none;font-weight:700}
@container (min-width:240px){.cd{grid-template-columns:96px 1fr;align-items:center}.ph{height:76px}}@container (max-width:150px){.tx span{display:none}.ph{height:34px}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<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>
</div>
<script>
const hold=document.getElementById('hold'),wr=document.getElementById('wr');
function up(){hold.style.setProperty('--w',wr.value+'px');const w=hold.clientWidth-18;document.getElementById('wv').textContent=wr.value+'px';document.getElementById('out').textContent='Content width '+w+'px: '+(w>=240?'side-by-side layout.':w<=150?'compact layout, description hidden.':'stacked layout.');}
wr.addEventListener('input',up);up();
</script>
</body></html>