CSS Container Query Card
A card that switches from stacked to side by side based on its container’s width, not the viewport.
Container queries
This card lays itself out by its own width.
<div class="cq-wrap">
<article class="cq-card">
<div class="cq-media"></div>
<div>
<h3>Container queries</h3>
<p>This card lays itself out by its own width.</p>
</div>
</article>
</div>
.cq-wrap {
container-type: inline-size;
}
.cq-card {
display: grid;
gap: 10px;
padding: 10px;
border: 1px solid #e0e7ff;
border-radius: 14px;
background: #fff;
font-family: system-ui, sans-serif;
}
.cq-media {
min-height: 72px;
border-radius: 10px;
background: linear-gradient(135deg, #a5b4fc, #6366f1);
}
.cq-card h3 {
margin: 0 0 4px;
color: #1e1b4b;
font-size: 15px;
}
.cq-card p {
margin: 0;
color: #64748b;
font-size: 13px;
line-height: 1.4;
}
/* Side by side once the container is wide enough */
@container (min-width: 300px) {
.cq-card {
grid-template-columns: 96px 1fr;
align-items: center;
gap: 14px;
}
.cq-media {
aspect-ratio: 1;
}
}
About this CSS Container Query Card
This CSS Container Query Card is 45 lines of pure CSS (0.7 KB) with no JavaScript and no dependencies. It uses linear gradients, CSS Grid, container queries and aspect-ratio. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- linear gradients
- CSS Grid
- container queriesChrome 105+, Safari 16+, Firefox 110+.
- aspect-ratio
Customize it
The colors in this snippet. Search and replace them to match your brand:
#e0e7ff#fff#a5b4fc#6366f1#1e1b4b#64748b
How to use this container query card
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.cq-wrapclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.