Container Queries for a Card Used in Two Different Layouts
Container queries are useful when the same card appears in a wide article area and a narrow sidebar. Both cards share the viewport width, but they do not have the same room.
A viewport media query cannot tell those situations apart. Query the card’s parent instead.
View code
<label>Wide container <input id="width" type="range" min="220" max="580" value="560"> <output id="value">560px</output></label>
<div class="slot adjustable"><article class="card"><div class="marker" aria-hidden="true">A</div><div><h2>A reusable card</h2><p>The card responds to its container.</p></div></article></div>
<div class="slot narrow"><article class="card"><div class="marker" aria-hidden="true">A</div><div><h2>A reusable card</h2><p>The card responds to its container.</p></div></article></div>button, select { padding: .35rem .6rem; border: 1px solid var(--clg); background: var(--cw); color: var(--cb); }
input[type="range"] { width: min(100%, 18rem); vertical-align: middle; }
h2, h3 { font-size: 1rem; }
.note { color: var(--clg); }
.slot { container: article-card / inline-size; margin-top: 1rem; max-width: 100%; border: 1px solid var(--clg); }
.adjustable { width: 560px; }
.narrow { width: 220px; }
.card { display: grid; gap: .75rem; padding: 1rem; }
h2, p { margin: 0; }
p { margin-top: .5rem; }
.marker { display: grid; place-items: center; min-height: 3rem; background: var(--cg); }
@container article-card (inline-size >= 26rem) { .card { grid-template-columns: 5rem minmax(0, 1fr); } }const width = document.querySelector('#width');
width.addEventListener('input', () => {
document.querySelector('.adjustable').style.width = `${width.value}px`;
document.querySelector('#value').value = `${width.value}px`;
});Give the card a named container
<div class="card-slot">
<article class="card">
<img src="photo.jpg" alt="A path through a pine forest"
width="800" height="600">
<div>
<h2>A walk outside Riga</h2>
<p>A short route with room to stop and take photographs.</p>
</div>
</article>
</div>
Save an image as photo.jpg beside the example page, then add:
.card-slot {
container: article-card / inline-size;
}
.card {
display: grid;
gap: 1rem;
}
.card img {
width: 100%;
height: auto;
}
@container article-card (inline-size >= 32rem) {
.card {
grid-template-columns: 12rem minmax(0, 1fr);
align-items: start;
}
}
The card stays stacked until its container has enough inline space for two columns. Its placement on the page does not need a separate sidebar class.
Query an ancestor, style a descendant
The separate .card-slot matters. A size query cannot use an element as its own query container. Defining the container on the wrapper avoids trying to size the card based on itself.
The name makes the intended ancestor explicit if other containers are introduced later. MDN’s guide to size queries explains container selection and containment.
Keep the narrow layout useful
The unqueried layout is also the fallback for browsers without size-query support. It still contains the image, heading, and description.
Choose the breakpoint where the card’s content needs a different layout. Test long headings and translated labels, which may need more room than the sample text in a mockup.