Container Queries: Make an AI-Generated Card Work in a Sidebar and a Full-Width Row
Disclosure: I work on VibUI, a paid library of website prompts. It's linked once, at the end. You've got a lovely card. Image on the left, text on the right, looks great in the main column. Then you drop the same card i
Disclosure: I work on VibUI, a paid library of website prompts. It's linked once, at the end.
You've got a lovely card. Image on the left, text on the right, looks great in the main column. Then you drop the same card into the sidebar and it squashes to a ribbon, with the image eating half the width and the title wrapping every second word.
The tool built the layout with media queries, which look at the screen. But the card doesn't care about the screen. It cares about how much room its own parent gave it. That's what container queries are for.
The fix in a few lines
.card-slot {
container-type: inline-size; /* make the parent a query container */
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 480px) {
.card {
grid-template-columns: 160px 1fr; /* image beside text only when there is room */
}
}
<div class="card-slot">
<article class="card">
<img src="photo.webp" alt="" width="320" height="200">
<div>
<h3>Card title</h3>
<p>Short description.</p>
</div>
</article>
</div>
Now the card stacks when it's narrow and goes side by side when it's wide, wherever you put it. A component that responds to its own space is far easier to reuse.
Media query or container query?
| Situation | Use |
|---|---|
| Page-level layout (columns, header, footer) | Media query |
| Reusable component that appears in different places | Container query |
| Typography that scales with the screen | Fluid sizes or media queries |
| A card that sits in a sidebar and a main column | Container query |
Gotchas
- The query container is the parent. An element can't query its own size, which is why
.card-slotexists. -
container-type: inline-sizemakes the container size independent of its children's width, so give it a sensible width from the layout. - Support is good in current browsers, but check your audience's before relying on it, and make the stacked layout the default so older browsers still get something usable.
- Name containers when you have several:
container: card / inline-size;then@container card (min-width: 480px).
Prompt block
Build the card as a reusable component. Default layout is a single stacked column.
Wrap it in a container with container-type: inline-size and use @container queries
(not @media) to switch to image-beside-text when the container is at least 480px wide.
Don't set widths inside the component; let the parent decide.
It works in v0, Lovable, Bolt, Cursor, Windsurf, Claude, ChatGPT, Gemini, Replit, GitHub Copilot, Framer AI, Google Stitch, Readdy, Aura.build, OpenAI Codex. If a tool keeps writing @media for components, name the technique in the prompt. They know it, they just don't always reach for it.
Check it
- [ ] Put the component in a narrow sidebar, a wide column and a three-up grid
- [ ] Drag the container narrower and watch the switch
- [ ] Zoom to 200 percent
- [ ] Look at it in a browser that lacks support, if you need to support one
A small plug
Reusable pieces matter most when you assemble pages from parts. If you work that way: VibUI has 2,852 website prompt templates: 775 pages, 1,100 sections, 817 components, 160 animated backgrounds, about 1,000 with source code. One-time payments, no auto-renewal: $48 quarterly, $99 yearly, $179 lifetime (down from $384); yearly and lifetime include 2,407 AI video prompts. 60 free samples, and a free account can copy 2 prompts. Open a free component sample with source code and try it in two different slots.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.