CSS Container Queries

By

Tagged: CSS

CSS container queries style a component by its own width, not the viewport. Learn container-type, named containers, query units, and the traps that break layouts.

A card might appear in a narrow sidebar, a wide main column, or one cell in a three-column grid. The viewport can stay the same while the card’s available space changes.

Media queries look at the viewport. That’s the right tool for the page: hide a nav, switch a 3-column grid to 1 column, or change the type size of the whole article.

That approach breaks down for reusable components. If you write @media (min-width: 700px) on a card, it also “goes wide” when it sits in a 240px sidebar on a large screen. The wide window tells you nothing about the card’s actual width.

Container queries fix that. You mark a wrapper as a container, then write rules that measure that wrapper instead of the window.

I use them whenever a component has to survive more than one slot, and keep media queries for the page layout around it.

The Brew Notes card exercise tests two copies at different widths and verifies the container threshold without resizing the viewport.

Mark a wrapper as a container

Container queries measure an ancestor of the element you want to style.

Give the wrapper container-type: inline-size. That tells the browser to track this element’s width, because something inside will ask about it:

.card-slot {
  container-type: inline-size;
}

inline-size is the value you want almost every time. In a horizontal writing mode, inline size is the width.

There’s also size, which tracks width and height. Skip it unless you truly need a height query, because size applies containment on both axes. A box that used to grow with its content can collapse once the browser isn’t allowed to let descendants define its height.

normal is the default, meaning the element isn’t a size container.

The container shorthand sets a name and a type in one line:

.card-slot {
  container: card / inline-size;
}

That’s the same as container-name: card plus container-type: inline-size. We’ll use the name in a minute.

Query the container, not the viewport

@container works a lot like @media. The condition looks similar, except you’re measuring the container instead of the viewport:

.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@container (min-width: 400px) {
  .card {
    flex-direction: row;
  }
}

When the nearest size container is at least 400px wide, the card goes side by side. When it’s narrower, the card stacks. You can resize the sidebar without changing the viewport.

You can also write the newer range syntax:

@container (width > 400px) {
  .card {
    flex-direction: row;
  }
}

It works the same way. I still use min-width a lot because it matches how I already write media queries.

Conditions measure the container’s content box, so padding on the wrapper isn’t part of the number.

The query styles descendants, not the container

This is the rule that catches most people off guard: @container applies to elements inside the container, not the container itself.

So this won’t do anything useful:

.card-slot {
  container-type: inline-size;
}

@container (min-width: 400px) {
  .card-slot {
    padding: 2rem;
  }
}

.card-slot is the container, so it can’t match a rule that only applies to its descendants.

Put the query on the component inside instead:

<div class="card-slot">
  <article class="card">
    <img src="/images/newsletter.jpg" alt="">
    <div class="card-body">
      <h2>Weekly notes</h2>
      <p>One email on shipping software.</p>
    </div>
  </article>
</div>
.card-slot {
  container-type: inline-size;
}

.card {
  display: flex;
  flex-direction: column;
}

@container (min-width: 400px) {
  .card {
    flex-direction: row;
  }

  .card img {
    width: 10rem;
    object-fit: cover;
  }
}

A dedicated wrapper keeps the roles clear. The slot is the container, and the card inside it changes.

This pairs well with CSS Grid for the page and Flexbox inside the card. Grid decides how wide each slot is, and the container query decides how the card uses that width.

A card that works in two slots

Here’s the situation I hit most often: the same card placed in two different spots on one page:

<div class="page">
  <main class="main">
    <div class="card-slot">
      <article class="card">...</article>
    </div>
  </main>
  <aside class="sidebar">
    <div class="card-slot">
      <article class="card">...</article>
    </div>
  </aside>
</div>
.page {
  display: grid;
  grid-template-columns: 1fr 16rem;
  gap: 2rem;
}

.card-slot {
  container-type: inline-size;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@container (min-width: 400px) {
  .card {
    flex-direction: row;
    align-items: center;
  }
}

On a wide window the main column is well above 400px, so that card goes horizontal. The sidebar is 16rem, so that card stays stacked. The same HTML works in both slots without an extra class, a card--compact modifier, or JavaScript measuring the node.

If you only have one layout and the card always spans the page, a media query is enough.

Named containers

Without a name, @container walks up to the nearest size container.

That’s fine until you nest them. A widget inside a card inside a sidebar can accidentally listen to the card instead of the sidebar.

Name the one you want to target:

.sidebar {
  container: sidebar / inline-size;
}

.card-slot {
  container: card / inline-size;
}

@container sidebar (min-width: 300px) {
  .widget {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

Now the widget ignores the card container above it and only checks the sidebar container.

You can also write a name-only query with no size condition:

@container sidebar {
  .widget-title {
    font-size: 0.85rem;
  }
}

That matches any element living inside a container named sidebar. I rarely need a name-only query since I’m almost always checking the size too.

Container query units

vw is 1% of the viewport width. Container units are the same idea, pointed at the container:

UnitMeaning
cqw1% of the container width
cqh1% of the container height
cqi1% of the container inline size
cqb1% of the container block size
cqminthe smaller of cqi and cqb
cqmaxthe larger of cqi and cqb

I use cqi most since it follows writing mode like inline-size, often paired with clamp() for type:

.card-title {
  font-size: clamp(1rem, 4cqi, 1.5rem);
}

The title grows with the card rather than the window. A card in the sidebar stays small, while the same title in the main column scales up to 1.5rem and stops because clamp() caps it.

See CSS units for how these fit alongside rem, em, and vw.

If the browser can’t find a size container, these units fall back to the small viewport units (svw, svh, and friends). Whenever a type size looks “page sized” instead of “card sized,” you probably forgot container-type on the wrapper.

cqh and cqb only work when the container has container-type: size. With inline-size, height isn’t tracked for queries.

Why containment exists

Size queries need a hard number. If the container’s width depended on the child’s width, and the child’s width depended on the query, the browser would get stuck in a loop.

To prevent that, a size container applies inline-size containment, so the wrapper’s width comes from its parent instead of its children.

That’s usually what you want, since a grid cell or a sidebar column already has a width.

It bites you when the wrapper was shrink-wrapping its content:

.badge-wrap {
  display: inline-block;
  container-type: inline-size;
}

inline-block wants to size itself to its content, while containment says the content can’t define that width. You end up with a collapsed or broken box.

Put containment on an element that already has a definite width: a grid item, a flex item with a set basis, a column, or a wrapper with width: 100%.

If adding container-type changes your layout before you’ve even written an @container rule, pick a different wrapper.

Traps I keep hitting

How I would use this

Don’t sprinkle container-type on every div on a site.

Page chrome belongs to media queries. The header, the footer, the article column, and the breakpoint where a blog list shifts layout all follow the window.

Save container queries for reusable cards that show up in more than one slot.

A course teaser is the obvious candidate on this site. On /courses it can sit in a wide catalog section, while in a sidebar or a “keep learning” block at the end of a post, the same teaser stacks narrowly. Container queries let you handle both without maintaining a teaser--sidebar modifier you have to remember to add.

A tool card is the same story. The tools index is a grouped list today, so if I ever drop the same tool summary into a post footer and into a wide index grid, that component should check its slot instead of the viewport.

Put container-type: inline-size on the grid cell or the aside column rather than the card root. The cell already has a width, and the card stays a normal flex or grid child.

Skip container queries for a one-off landing section that always spans the article. Stick to @media and Flexbox there, since adding containment for a single slot just adds noise.

The free CSS course walks through media queries first and container queries second, so you learn how to adapt the page before adapting individual components.

For nesting, :has(), and the rest of the no-build-step list, see 12 CSS features you can use today.

Browser support

Container queries work in every current Chrome, Firefox, and Safari. You don’t need a polyfill for a site that already uses Grid and :has().

If you still have to support a very old browser, fall back to a simpler stacked card and a media query for the page. The card won’t adapt inside a sidebar, but it’ll still be readable.

Use container queries when the same component has to live at two different widths, and keep media queries for the page.

Want me to talk about your product? You can sponsor this site.

Related posts about css: