SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/container-queries

7 CSS Container Queries

7 Pure CSSPublished 2026-10-05Free · no sign-up

Seven demos of CSS container queries: a card that rearranges in narrow and wide slots, headings that scale with cqi units, a named container, style queries on custom properties, one widget adapting to three slot sizes, a resizable box to test with and containers nested inside containers.

01. Card That Rearranges by Its Width

Container queriesCard

The same card component placed in a narrow and a wide slot: in the narrow one the image sits on top, in the wide one beside the text. The card's wrapper is the container, so the card responds to its slot, not to the window.

View code
<div class="cq1"><div class="slot n"><article class="card"><div class="im" aria-hidden="true"></div><div><b>Rain jacket</b><p>Packs into its own pocket.</p></div></article></div><div class="slot w"><article class="card"><div class="im" aria-hidden="true"></div><div><b>Rain jacket</b><p>Packs into its own pocket.</p></div></article></div></div>
.cq1 { display: flex; flex-wrap: wrap; gap: 10px; width: 560px; max-width: 100%; font: 13px Inter, system-ui, sans-serif; }
.cq1 .slot { container-type: inline-size; } .cq1 .n { flex: 1 1 130px; } .cq1 .w { flex: 3 1 260px; }
.cq1 .card { display: grid; gap: 10px; padding: 10px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; }
.cq1 .im { aspect-ratio: 4 / 3; border-radius: 8px; background: linear-gradient(135deg, #fde68a, #f59e0b); }
.cq1 b { color: #101828; } .cq1 p { margin: 2px 0 0; color: #475467; }
@container (min-width: 240px) { .cq1 .card { grid-template-columns: 110px 1fr; align-items: center; } }

02. Fluid Type with Container Units

Container queriescqi

cqi is one percent of the container's inline size, so clamp(18px, 8cqi, 40px) makes a headline scale with the box it sits in. Drag the handle on the box to resize it and the heading resizes with it, the text in the paragraph stays put.

View code
<div class="cq2"><p class="h">Drag the corner</p><p>The heading uses cqi units.</p></div>
.cq2 { container-type: inline-size; width: 280px; min-width: 160px; max-width: 100%; padding: 16px; box-sizing: border-box; border: 1px dashed #475569; border-radius: 12px; resize: horizontal; overflow: auto; font-family: Inter, system-ui, sans-serif; }
.cq2 .h { margin: 0 0 6px; color: #fff; font-size: clamp(18px, 10cqi, 44px); font-weight: 900; line-height: 1.1; }
.cq2 p { margin: 0; color: #94a3b8; font-size: 13px; }

03. Named Containers

Container queriesNames

With nested containers, a query matches the nearest one by default. container-name lets an inner component ask about a specific ancestor, here the sidebar, rather than its immediate wrapper.

View code
<div class="cq3"><aside class="side"><div class="widget"><div class="inner"><b>Weather</b><span>18°C, clear</span></div></div></aside></div>
.cq3 { width: 420px; max-width: 100%; font: 13px Inter, system-ui, sans-serif; }
.cq3 .side { container: sidebar / inline-size; padding: 10px; border: 1px dashed #98a2b3; border-radius: 12px; }
.cq3 .widget { container-type: inline-size; padding: 10px; border: 1px dashed #d0d5dd; border-radius: 10px; }
.cq3 .inner { display: grid; gap: 2px; padding: 12px; border-radius: 8px; background: #eef4ff; color: #1e3a8a; }
.cq3 b { color: #101828; }
@container sidebar (min-width: 300px) { .cq3 .inner { grid-template-columns: 1fr auto; align-items: center; background: #ecfdf3; color: #054f31; } }

04. Style Queries on Custom Properties

Container queriesStyle

@container style(--tone: warm) applies styles when an ancestor sets a custom property to a value, a way to theme components without extra classes. Browsers without style queries keep the default look, so it degrades gracefully.

View code
<div class="cq4"><div class="wrap" style="--tone: warm"><span class="badge">Warm tone</span></div><div class="wrap" style="--tone: cool"><span class="badge">Cool tone</span></div></div>
.cq4 { display: flex; flex-wrap: wrap; gap: 10px; font: 700 14px Inter, system-ui, sans-serif; }
.cq4 .wrap { container-name: tone; }
.cq4 .badge { display: inline-block; padding: 10px 16px; border-radius: 999px; background: #334155; color: #e2e8f0; }
@container style(--tone: warm) { .cq4 .badge { background: #c2410c; color: #fff7ed; } }
@container style(--tone: cool) { .cq4 .badge { background: #0e7490; color: #ecfeff; } }

05. One Component, Three Slots

Container queriesReuse

A stat widget dropped into three slots of a dashboard: tiny, medium and wide. It shows only the number when tiny, adds a label when medium and adds a sparkline when wide, all from @container rules inside the component's own CSS.

View code
<div class="cq5"><div class="s s1"><div class="stat"><b>1,204</b><span class="l">Orders</span><span class="sp" aria-hidden="true"></span></div></div><div class="s s2"><div class="stat"><b>1,204</b><span class="l">Orders</span><span class="sp" aria-hidden="true"></span></div></div><div class="s s3"><div class="stat"><b>1,204</b><span class="l">Orders this week</span><span class="sp" aria-hidden="true"></span></div></div></div>
.cq5 { display: grid; grid-template-columns: 80px 140px minmax(0, 1fr); gap: 8px; width: 520px; max-width: 100%; font: 13px Inter, system-ui, sans-serif; }
.cq5 .s { container-type: inline-size; }
.cq5 .stat { display: grid; gap: 2px; padding: 10px; border: 1px solid #e4e7ec; border-radius: 10px; background: #fff; }
.cq5 b { color: #101828; font-size: 18px; } .cq5 .l, .cq5 .sp { display: none; } .cq5 .l { color: #475467; }
.cq5 .sp { height: 26px; background: linear-gradient(transparent 60%, #eef4ff 60%), repeating-linear-gradient(90deg, #528bff 0 3px, transparent 3px 8px); -webkit-mask: linear-gradient(to top, #000 30%, transparent); mask: linear-gradient(to top, #000 30%, transparent); }
@container (min-width: 120px) { .cq5 .l { display: block; } }
@container (min-width: 200px) { .cq5 .sp { display: block; } .cq5 b { font-size: 24px; } }
@media (max-width: 420px) { .cq5 { grid-template-columns: 1fr 1fr; } .cq5 .s3 { grid-column: 1 / -1; } }

06. Resizable Box to Test Queries

Container queriesInteractive

A product tile inside a box you can resize by dragging its corner, so you can watch it switch between a stacked layout and a row as it crosses 300px. resize: horizontal with overflow: auto makes any element resizable for testing.

View code
<div class="cq6"><div class="tile"><div class="im" aria-hidden="true"></div><div class="tx"><b>Ceramic vase</b><p>$48 · In stock</p></div><button>Add</button></div></div>
.cq6 { container-type: inline-size; width: 260px; min-width: 150px; max-width: 100%; padding: 10px; box-sizing: border-box; border: 1px dashed #475569; border-radius: 12px; resize: horizontal; overflow: auto; font: 13px Inter, system-ui, sans-serif; }
.cq6 .tile { display: grid; gap: 10px; padding: 10px; border-radius: 10px; background: #1e293b; }
.cq6 .im { aspect-ratio: 1; border-radius: 8px; background: radial-gradient(ellipse at 50% 60%, #fca5a5, #b91c1c); }
.cq6 b { color: #fff; } .cq6 p { margin: 2px 0 0; color: #94a3b8; }
.cq6 button { padding: 8px; border: 0; border-radius: 8px; background: #f8fafc; color: #0f172a; font: 700 13px Inter, system-ui, sans-serif; cursor: pointer; }
.cq6 button:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }
@container (min-width: 300px) { .cq6 .tile { grid-template-columns: 70px 1fr auto; align-items: center; } }

07. Nested Containers in a Layout

Container queriesNesting

A page region that is itself a container holds cards that are containers too. The region switches from one column to two at its own breakpoint, and each card then switches its inside layout at another, two independent levels of responsiveness.

View code
<section class="cq7"><div class="grid"><div class="cw"><article><span class="ic" aria-hidden="true">✦</span><div><b>Insights</b><p>Weekly trends in one email.</p></div></article></div><div class="cw"><article><span class="ic" aria-hidden="true">◆</span><div><b>Exports</b><p>CSV, PDF and Sheets.</p></div></article></div></div></section>
.cq7 { container-type: inline-size; width: 560px; max-width: 100%; font: 13px Inter, system-ui, sans-serif; }
.cq7 .grid { display: grid; gap: 8px; }
@container (min-width: 420px) { .cq7 .grid { grid-template-columns: 1fr 1fr; } }
.cq7 .cw { container-type: inline-size; }
.cq7 article { display: grid; gap: 8px; padding: 14px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; }
.cq7 .ic { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; background: #f4ebff; color: #6941c6; font-size: 18px; }
.cq7 b { color: #101828; } .cq7 p { margin: 2px 0 0; color: #475467; }
@container (min-width: 230px) { .cq7 article { grid-template-columns: auto 1fr; align-items: center; } }

About these CSS Container Queries

Make a container

Set container-type: inline-size on the element whose width should be measured, usually the component's wrapper or slot. Then @container (min-width: 300px) rules apply to its descendants.

A container cannot query itself

Rules inside @container style the container's children, not the container. Put the container on a wrapper around the element you want to change.

Units that follow the container

cqi, cqw and friends are percentages of the container's size. clamp(18px, 8cqi, 40px) gives type that scales with the component, within limits.

Name containers when they nest

Queries match the nearest container. Give a specific ancestor a name with container-name (or the container shorthand) and query it by name to skip the ones in between.

Frequently asked questions

Are container queries supported?

Size queries and container units work in all current browsers. Style queries work in Chromium and Safari.

Do they replace media queries?

For components, mostly. Media queries are still right for page-level choices and user preferences like reduced motion.

Can I query height?

Yes, with container-type: size, but the container then needs an explicit height.