On this page10 sections
Lecture 055: Container Queries
| Category | 9: Responsive Design |
| Lecture | 055 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Media queries ask "how wide is the screen?" But a card might sit in a wide main column on one page and a narrow sidebar on another. Container queries ask "how wide is my container?", so a component adapts to wherever you put it.
Learning Objectives
- Make an element a query container with
container-type - Write
@containerrules - Name containers with
container-name - Size things with container units such as
cqi
1. The Two Steps
- Mark the parent as a container:
container-type: inline-size; - Style the children with
@container (min-width: …) { … }
Example: the same card in a narrow and a wide container:
<style>
.slot { container-type: inline-size; border: 1px dashed #adb5bd; padding: 8px; margin-bottom: 10px; }
.card { display: grid; gap: 10px; background: white; border: 1px solid #dee2e6; border-radius: 10px; padding: 10px; font-family: system-ui; }
.card img { width: 100%; border-radius: 8px; aspect-ratio: 16 / 9; object-fit: cover; }
@container (min-width: 380px) {
.card { grid-template-columns: 140px 1fr; align-items: center; }
.card img { aspect-ratio: 1; }
}
</style>
<div class="slot" style="width: 240px;">
<div class="card"><img src="https://picsum.photos/id/1080/400/300" alt="Strawberries"><div><strong>Narrow slot</strong><p>Image on top.</p></div></div>
</div>
<div class="slot" style="max-width: 520px;">
<div class="card"><img src="https://picsum.photos/id/1080/400/300" alt="Strawberries"><div><strong>Wide slot</strong><p>Image beside the text, using the same CSS.</p></div></div>
</div>Same card, same CSS, but a different layout depending on the space it has.
2. container-type Values
| Value | Can query |
|---|---|
inline-size | Width (the usual choice) |
size | Width and height (the container needs a set height) |
normal | Nothing (the default) |
3. Named Containers
When containers are nested, name the one you mean:
.sidebar { container: sidebar / inline-size; } /* name / type */
@container sidebar (max-width: 300px) {
.card h3 { font-size: 1rem; }
}4. Container Units
| Unit | Means |
|---|---|
cqi | 1% of the container's inline size (width) |
cqb | 1% of its block size (height) |
cqmin / cqmax | The smaller / larger of the two |
Example: the heading scales with its box, not the screen:
<style>
.box { container-type: inline-size; background: #f3f0ff; padding: 10px; margin-bottom: 8px; border-radius: 8px; }
.box h3 { margin: 0; font-size: clamp(1rem, 8cqi, 2.5rem); }
</style>
<div class="box" style="width: 200px;"><h3>Small box</h3></div>
<div class="box"><h3>Wide box</h3></div>5. Media or Container Query?
| Use a media query for… | Use a container query for… |
|---|---|
| The overall page layout (columns, navigation) | Reusable components (cards, widgets) |
Device features (prefers-color-scheme, hover) | Anything that should fit its slot |
They work well together.
Common Beginner Mistakes
1. Forgetting container-type. @container rules do nothing without a container.
2. Querying the element itself. A container query styles the container's children, not the container.
3. container-type: size without a height. The container collapses. Use inline-size for width-only queries.
Practice Questions
- Which property makes an element a query container?
- Write a container query for containers at least 400px wide.
- What does a container query look at, compared with a media query?
- How do you name a container
main? - What is
1cqi? - Should a site's main navigation layout use a media query or a container query?
Practice Answers
container-type(usuallyinline-size).@container (min-width: 400px) { … }- The size of the nearest container, instead of the size of the screen.
container-name: main;(orcontainer: main / inline-size;)- 1% of the container's width.
- A media query: it depends on the whole screen.
Key Takeaways
container-type: inline-sizeon the parent,@container (…)for the children.- Components adapt to their own space, wherever they're used.
- Name containers when nesting; size with
cqiunits. - Media queries for pages, container queries for components.