Skip to content
codmaker

CSS Container Queries: A Practical Guide

How CSS container queries work, the syntax you need, container units, how they differ from media queries, and the gotchas that trip people up in real layouts.

CodmakerUpdated 8 min read
Share

What Are CSS Container Queries?

Container queries let an element change its styles based on the size of its parent container instead of the size of the browser window. Responsive design stops being only about the viewport. It becomes about the context a component lives in.

Think of a card component. In a wide main grid it should show the image next to the text. In a narrow sidebar it should stack. With media queries alone, the card has no idea where it is. It only knows the screen width.

Container queries let the component say: if I have less than 300px, stack vertically. The rule lives with the component, so the component becomes truly portable. Drop it anywhere and it adapts.

Container Queries vs Media Queries

Media queries ask a question about the page: how wide is the viewport, is the user in dark mode, do they prefer reduced motion. They are still the right tool for page-level layout, like switching from one column to a multi-column shell.

Container queries ask a question about a specific ancestor: how much room do I actually have here? That's the right tool for components. A component used in three different layouts doesn't care about the screen. It cares about its slot.

In practice you use both. Media queries shape the page. Container queries shape the pieces inside it.

How to Use @container: Syntax Basics

There are two steps. First, mark the parent as a container with container-type: inline-size. Second, write an @container rule for the children, like @container (min-width: 400px) { ... }. That's the core of it.

The inline-size value means the container tracks its width in horizontal writing modes. There is also container-type: size, which tracks both width and height, but that one needs the container to have an explicit height, so inline-size is what you want most of the time.

You can also use the modern range syntax: @container (width > 400px) reads more naturally than min-width and works the same way. Inside the query you can test width, height, inline-size, block-size, aspect-ratio and orientation.

  • Parent: .card-slot { container-type: inline-size; }
  • Child: @container (min-width: 400px) { .card { display: grid; grid-template-columns: 1fr 2fr; } }
  • Default styles outside the query should be the narrow, stacked version.

Named Containers and the container Shorthand

By default, an @container rule matches the nearest ancestor that is a container. That's usually what you want. When containers are nested, you sometimes need to target a specific one.

Give it a name with container-name: sidebar, then query it with @container sidebar (max-width: 300px). The shorthand container: sidebar / inline-size sets the name and the type in one line.

Naming is also good documentation. When someone reads @container sidebar, they know exactly which box the rule depends on.

A practical pattern: make layout slots the containers, not the components. The sidebar, the main column and each grid cell declare themselves as containers. Components inside them only write @container rules. That way a component never has to know which layout it was dropped into.

Container Query Units: cqi, cqw and Friends

Container queries come with their own length units. They work like viewport units, except they are relative to the query container instead of the window.

The ones you'll use most are cqi (1% of the container's inline size) and cqw (1% of its width). There are also cqh, cqb, cqmin and cqmax. A common trick is fluid type that scales with the component, like font-size: clamp(1rem, 4cqi, 1.5rem). The heading grows in a wide card and shrinks in a narrow one, without a single breakpoint.

Common Mistakes and Gotchas

Most container query bugs come from the same few misunderstandings. Knowing them up front saves an afternoon of staring at DevTools.

  • An element can't query itself. The container must be an ancestor, so you often need a wrapper element.
  • container-type: inline-size applies size containment on that axis. The container no longer sizes itself from its content's width, so a container inside a shrink-to-fit parent (like an inline-block or some flex items) can collapse. Give it a width or let it stretch.
  • Forgetting to declare a container at all. Without container-type on an ancestor, the @container rule silently never matches.
  • Using container-type: size without a defined height, which collapses the element vertically.
  • Replacing every media query. Page-level layout still belongs in media queries.

Browser Support and Using Them Today

Size-based container queries and container units are supported across current versions of all the major browsers, so you can use @container in production today. If you need to support very old browsers, write the stacked layout as the default and let the query enhance it. Older browsers just get the simpler version.

Style queries, which match on custom property values with @container style(--variant: compact), are a separate feature with less complete support. Check current support before relying on them.

Frameworks have caught up too. Tailwind CSS supports container queries with an @container class on the parent and size variants on the children. In a Next.js or React project, they pair well with a component library, because each component carries its own responsive rules wherever it's rendered.

Frequently Asked Questions

What is the difference between container queries and media queries?

Media queries respond to the viewport or device. Container queries respond to the size of a parent element. Use media queries for page layout and container queries for components that appear in different contexts.

Are CSS container queries supported in all browsers?

Size container queries and container query units work in current versions of all major browsers. Style queries are newer and have less complete support, so check before you depend on them.

Why is my @container query not working?

The most common causes: no ancestor has container-type set, you are trying to query the element itself instead of a parent, or the container has collapsed to zero width because inline-size containment stops it from sizing to its content.

What does container-type: inline-size do?

It turns the element into a query container that tracks its inline size, which is its width in horizontal writing modes. Children can then use @container rules and container units based on that width.

Can I use container queries with Tailwind CSS?

Yes. Add the @container class to the parent and use container size variants on the children. Named containers are supported as well.

Written by Codmaker

An independent product lab. We build and run our own React Native and Next.js apps, and write up what we learn along the way.

See our apps
All guides →