CSS Container Queries Playground — Responsive Card Example
Resize one component and see a CSS container query switch its layout.
The live editor loads here with JavaScript enabled.
Run in Playground ↗The original source and explanation are available below.
Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.
Useful? Help keep TryJS free and independent.
Support TryJSWhat to expect
A wide card uses two columns. Below 380px it stacks its illustration and text, independently of the full page width.
About this example
Container queries let a component adapt to the space given to it. That is useful when the same card appears in a wide content area and a narrow sidebar. The slider changes the component container rather than the browser viewport.
How It Works
Create a query container
container-type: inline-size makes the wrapper's width available to descendant container queries.
Change the component layout
The @container rule changes the inner card's grid when its wrapper is narrower than 380px.
Read the original source
This is the starting code. Your edits stay in the live editor above.
HTML
<main><h1>A card that fits</h1><label>Container width <input id="width" type="range" min="240" max="600" value="500" /></label><div class="frame"><article class="card"><div class="art" aria-hidden="true">✦</div><div><h2>Built for its space</h2><p>This card responds to its container.</p></div></article></div></main>CSS
* { box-sizing: border-box; }
body { margin: 0; padding: 24px; background: #faf8f0; color: #222; font: 15px/1.6 system-ui, sans-serif; }
h1 { font-size: 24px; line-height: 1.2; margin: 0 0 12px; }
p { margin: 8px 0 16px; }
button, input { font: inherit; }
button { padding: 9px 16px; border: 1px solid #b9a200; border-radius: 7px; background: #f0d000; color: #222; cursor: pointer; }
button:disabled { opacity: .6; cursor: wait; }
input { max-width: 100%; padding: 9px 12px; border: 1px solid #aaa; border-radius: 7px; }
small { color: #595959; }
output { font-variant-numeric: tabular-nums; }
.frame { container-type: inline-size; width: 500px; max-width: 100%; margin-top: 20px; }
.card { display: grid; grid-template-columns: 110px 1fr; gap: 18px; align-items: center; padding: 18px; border: 1px solid #ccc; border-radius: 12px; background: white; }
.art { display: grid; place-items: center; min-height: 100px; border-radius: 8px; background: #f0d000; font-size: 40px; }
h2 { font-size: 18px; margin: 0; }
@container (max-width: 380px) { .card { grid-template-columns: 1fr; } }JavaScript
document.querySelector("#width").addEventListener("input", event => {
document.querySelector(".frame").style.width = event.target.value + "px";
});