Sub topic 10
Putting it together: a component that adapts to its space
This chapter assumes you know container-type, the @container rule, and container query units from the earlier chapters. You will build one card component and place it in two different layout slots. The card adapts to each slot with no viewport media queries.
The goal
A card shows an image, a heading, and some text. In a wide slot, the image sits beside the text. In a narrow slot, the image stacks above the text. The same card is used in a main column and in a sidebar, so the viewport width tells you nothing about how much room the card has.
Step 1: markup
The card needs a wrapper that acts as the query container. A container cannot style itself, so the card must be a descendant of the wrapper.
<main class="layout">
<section class="card-wrapper">
<article class="card">
<img src="photo.jpg" alt="A lighthouse at dusk">
<div>
<h2>Lighthouse tour</h2>
<p>Climb 120 steps for a view of the coast.</p>
</div>
</article>
</section>
<aside class="sidebar">
<div class="card-wrapper">
<article class="card">
<img src="photo.jpg" alt="A lighthouse at dusk">
<div>
<h2>Lighthouse tour</h2>
<p>Climb 120 steps for a view of the coast.</p>
</div>
</article>
</div>
</aside>
</main>Step 2: write the narrow layout first
Start with the stacked layout as the default. Then add the wider layouts inside @container rules, the same mobile-first habit you use with media queries.
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1.5rem;
}
.card-wrapper {
container: card / inline-size;
}
.card {
display: grid;
gap: 1rem;
padding: 1rem;
}
.card img {
width: 100%;
height: auto;
}
.card h2 {
margin: 0;
font-size: 1.25rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}
@container card (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
align-items: start;
}
}
@container card (min-width: 700px) {
.card {
grid-template-columns: 12rem 1fr;
padding: 1.5rem;
}
}Three details matter here:
- The
containershorthand names the containercard. Both wrappers share the name, but each rule is evaluated against the nearest ancestor with that name, so each card reads its own wrapper. - The heading declares
font-size: 1.25remfirst. A browser that does not understandcqidrops the second declaration and keeps the first. - The
cqivalue is resolved against the card's wrapper, not the viewport. A 280px sidebar gives about 11px, soclamp()raises it to the 1.125rem minimum.
Step 3: keep a safe fallback
If you must support browsers without container queries, put the enhancements behind @supports. The base styles already give a usable stacked card.
@supports (container-type: inline-size) {
.card-wrapper {
container: card / inline-size;
}
@container card (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
}In the full stylesheet, you would move the two @container rules and the container declaration from Step 2 into this block.
Common errors
| Symptom | Likely cause |
|---|---|
| No container rule ever applies | The styled element is the container itself, or no ancestor has `container-type`. |
| Wrapper collapses to zero width | Inline-size containment removes content-based width. This often happens when the wrapper is a flex item with no basis. Give it `width: 100%` or `flex: 1`. |
| Rule matches the wrong container | A nearer ancestor with a container type (and a matching name, if you used one) is being picked. |
| Height conditions never match | The container uses `inline-size`, not `size`. |
Exercise
The sidebar wrapper is 280px wide and the main wrapper is 640px wide. Using the Step 2 CSS, which @container rules apply to the card in each slot, and why?
Show answer
In the sidebar, neither rule applies, because 280px is below 400px, so the card stays stacked. In the main column, only the min-width: 400px rule applies, because 640px is below 700px, so the card gets the 8rem image column. Each card is measured against its own nearest card container, not the viewport.