Responsive card with container queries in CSS
34 A component that responds to its container
hardMake a card lay out side by side when it has room and stack when it does not, based on the width of its container rather than the viewport. Put two copies in differently sized slots to show it.
Stuck? Three hints, smallest first
- 1. The parent needs
container-type: inline-size, or there is nothing to query. - 2.
@container (min-width: 20em)then styles the descendants, not the container itself. - 3. Name the container with
container-namewhen a component sits inside several.
<div class="slot slot--wide">
<article class="card">
<div class="card__thumb" aria-hidden="true"></div>
<div class="card__text">
<h3>Abuja</h3>
<p>12 hours, one stop at Lokoja.</p>
</div>
</article>
</div>
<div class="slot slot--narrow">
<article class="card">
<div class="card__thumb" aria-hidden="true"></div>
<div class="card__text">
<h3>Abuja</h3>
<p>12 hours, one stop at Lokoja.</p>
</div>
</article>
</div>
.slot {
container-type: inline-size; /* without this, @container never matches */
container-name: card;
margin-bottom: 10px;
padding: 6px;
border: 1px dashed #c9cad1;
border-radius: 8px;
}
.slot--wide { max-width: 340px; }
.slot--narrow { max-width: 170px; }
/* Base: stacked. The same component, whatever it is put inside. */
.card {
display: grid;
gap: 8px;
margin: 0;
font-size: 0.8rem;
}
.card__thumb {
height: 48px;
border-radius: 6px;
background: linear-gradient(135deg, #0b3a8f, #3055d8);
}
.card h3 { margin: 0 0 2px; font-size: 0.9rem; }
.card p { margin: 0; color: #5c5c66; }
/* Side by side, but only when the CONTAINER is wide enough. */
@container card (min-width: 18em) {
.card {
grid-template-columns: 70px 1fr;
align-items: center;
}
.card__thumb { height: 60px; }
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('the container is declared', /container-type:\s*inline-size/.test(css), true);
test('and named, so the query can target it', /container-name:\s*card/.test(css), true);
test('the query is a container query, not a media query', /@container card \(min-width: 18em\)/.test(css), true);
test('no media query is involved at all', /@media/.test(css), false);
test('the query styles descendants, not the container', /@container[^{]*\{\s*\.card \{/.test(css), true);
test('the base state is the stacked one', /\.card\s*\{[^}]*display:\s*grid/.test(css), true);
test('two copies of the same component in different slots', countTag(html, 'article'), 2);
test('the decorative thumbnail is hidden from assistive technology', /aria-hidden="true"/.test(html), true);
Output
The point of a container query is that the component stops caring where it is on the page. The same card here goes two-column in the wide slot and stacks in the narrow one, with no media query and no variant class, which is what a media query fundamentally cannot do: a sidebar at 300px inside a 1600px window is still a narrow context, and @media only sees the window. Two things catch people: container-type: inline-size is required on the parent, and the query styles the children, so styling the container from inside its own query does nothing.