Reading history

CSS Container Queries: Components That Fit Any Space

6 min read 0 views

Make components respond to their parent's width instead of the screen with container-type, @container rules, named containers and cqi units, with live demos.

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 @container rules
  • Named containers with the container shorthand
  • 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-size turns each .slot into 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 .slot is the container and the .product inside 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-size is a shorthand for container-name: panel and container-type: inline-size.
  • @container panel (min-width: 300px) targets the nearest ancestor named panel, 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.
  • cqi is 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 also cqw, cqh, cqmin and cqmax.

Media Queries or Container Queries?

UseForExample
Media queriesPage-level layout that depends on the screenSwitching from three columns to one on phones; hiding the sidebar
Container queriesComponents that appear in different placesCards, widgets, product tiles, comment boxes
Media featuresUser preferences and device abilitiesprefers-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

  1. Forgetting container-type, so @container rules never match.
  2. Trying to style the container itself inside its own query. Only descendants can be styled.
  3. Using container-type: size without giving the container a height. With size the container’s height no longer comes from its content, so it collapses; inline-size is usually what you want.
  4. 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-size on the wrapper, then write @container rules.
  • Name containers to target a specific ancestor; use cqi for container-relative sizes.
  • Use media queries for page layout and container queries for components.

Next: Modern CSS Selectors: :has(), :is(), :where() and Nesting.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *