Media queries change a design based on the size of the screen. But a component such as a product card may sit in a wide main column on one page and a narrow sidebar on another, on the same screen. Container queries let a component respond to the size of the box it is placed in. Supported in all modern browsers since 2023, they make truly reusable components possible.
In this lesson you will learn
- Why media queries are not enough for components
- Making an element a container with
container-type - Writing
@containerrules - Named containers with the
containershorthand - Container query units such as
cqi
You should know responsive design and media queries and CSS Grid.
Querying the Container
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Container queries</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 13px; }
.layout { display: grid; grid-template-columns: 150px 1fr; gap: 12px; }
.slot { container-type: inline-size; } /* this element can be queried */
.product { display: grid; gap: 8px; background: #f1f8e9; padding: 8px; border-radius: 6px; }
.thumb { background: #8bc34a; height: 50px; border-radius: 4px; }
/* When the slot is at least 260px wide, switch to a side-by-side card */
@container (min-width: 260px) {
.product { grid-template-columns: 90px 1fr; align-items: center; }
.thumb { height: 70px; }
}
</style>
</head>
<body>
<div class="layout">
<div class="slot">
<div class="product"><div class="thumb"></div><div><b>Mango</b><br>120 taka/kg</div></div>
</div>
<div class="slot">
<div class="product"><div class="thumb"></div><div><b>Mango</b><br>120 taka/kg</div></div>
</div>
</div>
<p>Same card, same CSS: narrow slot stacks, wide slot goes side by side.</p>
</body>
</html>
Result
container-type: inline-sizeturns each.slotinto a query container: its children may ask how wide it is. “Inline size” means width in normal left-to-right text.@container (min-width: 260px) { … }applies only when the nearest container is at least 260px wide.- Both cards use identical HTML and CSS. The one in the 150px slot stacks its image on top; the one in the wide slot shows it side by side. A media query could not do this, because the screen width is the same for both.
- A container cannot query itself, only style its children. That is why the wrapper
.slotis the container and the.productinside changes.
Named Containers and cq Units
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Named containers and cq units</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 13px; }
.panel { container: panel / inline-size; border: 2px dashed #90a4ae; padding: 8px; margin-bottom: 10px; }
.title { font-size: clamp(14px, 6cqi, 28px); margin: 0 0 6px; color: #37474f; }
.stats { display: grid; gap: 6px; grid-template-columns: 1fr; }
.stats div { background: #e3f2fd; padding: 6px; border-radius: 4px; }
@container panel (min-width: 300px) {
.stats { grid-template-columns: repeat(3, 1fr); }
}
</style>
</head>
<body>
<div class="panel" style="width: 220px">
<h3 class="title">Narrow panel</h3>
<div class="stats"><div>Students 40</div><div>Present 37</div><div>Absent 3</div></div>
</div>
<div class="panel" style="width: 420px; max-width: 100%">
<h3 class="title">Wide panel</h3>
<div class="stats"><div>Students 40</div><div>Present 37</div><div>Absent 3</div></div>
</div>
</body>
</html>
Result
container: panel / inline-sizeis a shorthand forcontainer-name: panelandcontainer-type: inline-size.@container panel (min-width: 300px)targets the nearest ancestor namedpanel, skipping any other containers in between. Names make large layouts with nested containers predictable.- The wide panel shows its three numbers in a row; the narrow one stacks them.
cqiis a container query unit: 1cqi is 1% of the container’s inline size.clamp(14px, 6cqi, 28px)makes the title grow with its panel, within limits. There are alsocqw,cqh,cqminandcqmax.
Media Queries or Container Queries?
| Use | For | Example |
|---|---|---|
| Media queries | Page-level layout that depends on the screen | Switching from three columns to one on phones; hiding the sidebar |
| Container queries | Components that appear in different places | Cards, widgets, product tiles, comment boxes |
| Media features | User preferences and device abilities | prefers-color-scheme, hover, prefers-reduced-motion |
The two work well together: media queries arrange the page, and container queries let each component adapt to the space it is given.
Common Mistakes
- Forgetting
container-type, so@containerrules never match. - Trying to style the container itself inside its own query. Only descendants can be styled.
- Using
container-type: sizewithout giving the container a height. Withsizethe container’s height no longer comes from its content, so it collapses;inline-sizeis usually what you want. - Querying a container whose width depends on its content, which can create circular logic. Containers should get their width from the layout around them.
Practice Exercises
Exercise 1. Make .widget a container and hide its .extra details when the widget is narrower than 200px.
Show answer
.widget { container-type: inline-size; }
@container (max-width: 199px) {
.extra { display: none; }
}
Exercise 2. Give a heading inside a container a size of 5% of the container’s width, but never below 16px or above 32px.
Show answer
h2 { font-size: clamp(16px, 5cqi, 32px); }
Frequently Asked Questions
Do container queries work in all browsers?
Size container queries and cq units work in all current versions of Chrome, Edge, Firefox and Safari, released from early 2023 onwards. For very old browsers, the component simply keeps its default layout.
What are style queries?
A newer kind of container query that checks a container’s CSS custom property, such as @container style(–theme: dark). All major browsers have supported them since 2026, but older versions do not, so treat them as an enhancement.
Summary
- Container queries respond to the size of a parent box, not the screen.
- Set
container-type: inline-sizeon the wrapper, then write@containerrules. - Name containers to target a specific ancestor; use
cqifor container-relative sizes. - Use media queries for page layout and container queries for components.
Next: Modern CSS Selectors: :has(), :is(), :where() and Nesting.
