← Back to HTML & CSS, page 3

html-css-0044 · Hard senior

What are container queries, and when would you use them instead of media queries?

A container query applies styles based on the size of a containing element, not the viewport. You mark an ancestor with container-type (usually inline-size), then write @container rules that style its descendants. This suits reusable components that appear in wide and narrow spaces. Media queries still suit page-level decisions.

Full answer

A media query asks about the viewport. A container query asks about the size of a containing element. That matches how component libraries work: a card might sit in a wide main column in one place and a narrow sidebar in another, on the same screen. A media query cannot tell the difference, but a container query can.

To use one, mark an ancestor as a query container with container-type. inline-size is the common value and tracks the width. Optionally name it with container-name, or use the shorthand container: card / inline-size. Then write @container card (min-width: 400px) { ... }. The query applies to descendants of the container, never to the container itself.

Setting container-type: inline-size applies size containment on the inline axis. The container's width then cannot depend on its content, so it needs to get its width from its parent or a CSS value. This avoids loops between size and style.

Container query length units, such as cqw (1 percent of the container's width), size things relative to the container.

The same .card markup shows a stacked layout in the 300px slot and a row layout in the 600px slot, on one page with one viewport width. Use media queries for page-level decisions, such as moving the whole sidebar or switching navigation style. Use container queries for reusable components.

Check the support table for the oldest browsers you serve. A reasonable fallback is to write the base layout so it is acceptable on its own, then enhance with the query.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  .slot { container: card / inline-size; margin-bottom: 12px; border: 1px dashed #999; }
  .card { display: flex; flex-direction: column; gap: 8px; padding: 12px; background: #eef; }
  .thumb { width: 100%; height: 40px; background: #99c; }
  @container card (min-width: 400px) {
    .card { flex-direction: row; align-items: center; }
    .thumb { width: 120px; flex: none; }
  }
</style>
<div class="slot" id="narrow" style="width: 300px"><div class="card" id="c1"><div class="thumb"></div><p>Intro to Python</p></div></div>
<div class="slot" id="wide" style="width: 600px"><div class="card" id="c2"><div class="thumb"></div><p>Intro to Python</p></div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • container-type: inline-size makes an element a query container for width.
  • @container rules affect descendants, never the container itself.
  • Container size cannot depend on its children's inline size.
  • cqw and related units are relative to the container.
  • Use media queries for page layout and container queries for components.

Common mistakes

  • forgetting container-type. Without it, the @container rule never matches anything.
  • trying to style the container itself inside its own @container block. Only descendants are affected, so add a wrapper inside.

Follow-ups

  • can a container query check more than width?
  • Why can a container not be styled by its own @container rule?
  • What is the difference between container-type: size and inline-size?

Tips

  • Query the smallest useful container, not the page. The component stays reusable if it only cares about its own space.
  • Use the card in a main column versus a sidebar as your example. It is clear and common.
  • Say you give the base layout a working default before enhancing with the query.