A card that looks good in the main column can feel cramped when you move it into a sidebar. Both cards share the same browser window, so a viewport breakpoint gives them the same signal even though their available widths differ. A CSS container query gives each copy of the card a breakpoint based on the width of its own slot. This example uses the same HTML in both locations and changes the layout with one rule.
Run the example
Download and extract the complete container query demo ZIP. Open index.html in a current browser that supports container size queries. Keep styles.css in the same directory. You can also create the two files from the full source below. The artwork comes from CSS gradients, so the example runs offline without image files, scripts or a build step.
At a 1024 CSS pixel viewport with the default font size, the main slot is 720 pixels wide and the sidebar is 256 pixels wide. The main card places the illustration beside its text. The sidebar puts the same illustration above the same text. At 390 pixels, the outer layout becomes one column. Both slots measure 358 pixels and both cards stack. Try both widths in your browser's responsive design view.
Give the page and the card separate breakpoints
The outer .layout uses a media query at 50rem. It starts with one track and adds a 16rem sidebar when the viewport has room. That decision belongs to the page. The outer grid determines the space available for each card.
The .slot wrapper declares container: card-slot / inline-size. The slash separates the name from the type. Naming the container makes the query look for the closest eligible ancestor called card-slot. The inline axis is horizontal in this example, so the container can answer a width query. The grid tracks supply the wrapper's available width. The card itself sits inside the wrapper and receives the query's styles.
Every card starts with a single grid track. Once its slot reaches 460 CSS pixels, the container rule assigns a 9rem track for the illustration and a flexible track for the copy. This is a design threshold for this particular card. If your illustration needs more space or your headings are longer, choose a threshold after testing your actual content.
Check the boundary and the fallback
Keep the viewport at 1024 pixels. In browser devtools, select the first .slot and set its inline-size to 459px. Its card stacks even though the page still has two columns. Change that value to 460px and the card returns to two tracks. The sidebar remains stacked. Minimum width includes the boundary, which makes the two measurements a quick way to check that the container rule is doing the work.
If the wide card stays stacked, confirm that the stylesheet loaded and inspect the wrapper's computed container name, type and content width. A name mismatch leaves the stacked base layout in place. A wrapper inside an auto-sized layout can also measure less than you expect when inline-size containment prevents its contents from supplying its intrinsic width. Give the wrapper space through the parent layout, as the grid does here. Keep the container on an ancestor of the card so the card can query it.
The copy track can shrink because it uses minmax(0, 1fr), and the text can wrap long uninterrupted strings because the copy has overflow-wrap: anywhere. Try replacing the description with a long identifier. The text should wrap inside the card. Both arrangements preserve the HTML order and use the same headings and text.
The two breakpoints can also produce horizontal cards in a single-column page. At a 600 pixel viewport with the default font size, the page has one track, but each slot has enough room for the horizontal card. That is expected. If you embed the component in a dialog or a dashboard panel, keep the slot wrapper with it so the component uses the space that panel provides.
Complete source
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Container query card demo</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main class="demo">
<h1>One card, two containers</h1>
<div class="layout">
<section class="slot" aria-labelledby="main-label">
<h2 id="main-label">Main column</h2>
<article class="card">
<div class="card-art" aria-hidden="true"></div>
<div class="card-copy">
<h3>Field notes</h3>
<p>Three ways to spot birds on a morning walk.</p>
</div>
</article>
</section>
<aside class="slot" aria-labelledby="side-label">
<h2 id="side-label">Sidebar</h2>
<article class="card">
<div class="card-art" aria-hidden="true"></div>
<div class="card-copy">
<h3>Field notes</h3>
<p>Three ways to spot birds on a morning walk.</p>
</div>
</article>
</aside>
</div>
</main>
</body>
</html>
styles.css
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #f7f4fb;
color: #292036;
font: 1rem/1.5 system-ui, sans-serif;
}
.demo {
max-inline-size: 64rem;
margin-inline: auto;
padding: 1rem;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 1rem;
}
@media (min-width: 50rem) {
.layout {
grid-template-columns: minmax(0, 1fr) 16rem;
}
}
.slot {
min-inline-size: 0;
container: card-slot / inline-size;
}
.card {
display: grid;
grid-template-columns: minmax(0, 1fr);
overflow: hidden;
border: 1px solid #cec1df;
border-radius: 0.75rem;
background: white;
}
.card-art {
min-block-size: 7rem;
background:
radial-gradient(circle at 70% 32%, #ffe3a4 0 13%, transparent 14%),
linear-gradient(155deg, #ad79c9 47%, #5b3f78 48% 63%, #322444 64%);
}
.card-copy {
min-inline-size: 0;
padding: 1rem;
overflow-wrap: anywhere;
}
.card-copy h3,
.card-copy p {
margin: 0;
}
.card-copy p {
margin-block-start: 0.35rem;
}
@container card-slot (min-width: 460px) {
.card {
grid-template-columns: 9rem minmax(0, 1fr);
}
}
Toolzify Editorial checked the desktop and mobile layouts, the 459 and 460 pixel boundary, a mismatched container name and long text in Chrome. The same checks passed after extracting the ZIP. See MDN's container query guide and the CSS Working Group draft for more on query containers.
Comments (0)
Join the discussion