On this page10 sections
  1. Learning Objectives
  2. 1. The Two Steps
  3. 2. container-type Values
  4. 3. Named Containers
  5. 4. Container Units
  6. 5. Media or Container Query?
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 055: Container Queries

Category9: Responsive Design
Lecture055 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

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 @container rules
  • Name containers with container-name
  • Size things with container units such as cqi

1. The Two Steps

  1. Mark the parent as a container: container-type: inline-size;
  2. Style the children with @container (min-width: …) { … }

Example: the same card in a narrow and a wide container:

html
<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

ValueCan query
inline-sizeWidth (the usual choice)
sizeWidth and height (the container needs a set height)
normalNothing (the default)

3. Named Containers

When containers are nested, name the one you mean:

css
.sidebar { container: sidebar / inline-size; }      /* name / type */

@container sidebar (max-width: 300px) {
  .card h3 { font-size: 1rem; }
}

4. Container Units

UnitMeans
cqi1% of the container's inline size (width)
cqb1% of its block size (height)
cqmin / cqmaxThe smaller / larger of the two

Example: the heading scales with its box, not the screen:

html
<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

  1. Which property makes an element a query container?
  2. Write a container query for containers at least 400px wide.
  3. What does a container query look at, compared with a media query?
  4. How do you name a container main?
  5. What is 1cqi?
  6. Should a site's main navigation layout use a media query or a container query?

Practice Answers

  1. container-type (usually inline-size).
  2. @container (min-width: 400px) { … }
  3. The size of the nearest container, instead of the size of the screen.
  4. container-name: main; (or container: main / inline-size;)
  5. 1% of the container's width.
  6. A media query: it depends on the whole screen.

Key Takeaways

  • container-type: inline-size on the parent, @container (…) for the children.
  • Components adapt to their own space, wherever they're used.
  • Name containers when nesting; size with cqi units.
  • Media queries for pages, container queries for components.