Container Queries vs. Media Queries: The Component-First Responsive Model

When to use container queries vs media queries: container-type, query units, style queries, and a component-first responsive model with real examples.

Executive Summary: Media queries ask about the viewport and the user’s device and preferences. Container queries ask about the space a component actually has. In a component-first responsive model, the page layout and user preferences stay in media queries, and every reusable component adapts to its container with size queries, container query units and, since May 2026, cross-browser style queries on custom properties. The result is a component that looks right in a sidebar, a modal or a main column without a single variant class.
Three container widths showing a component layout adapting to its own space

A product card looks great in the main column: image on the left, details on the right. Then a designer puts the same card in a 320px sidebar on a 1440px screen. The media query says “wide screen”, so the card uses its wide layout inside a narrow box, and the text gets crushed into a 90px column. The usual fix is a .card--compact modifier that the sidebar template must remember to add. Every new placement needs another modifier.

Media queries (@media) test conditions about the viewport, the device and the user: width, orientation, hover capability, color scheme preference, reduced motion. Container queries (@container) test conditions about an ancestor element that has been declared a container, most often its inline size. Size queries have been in every major browser since 2023. Style queries for custom properties became cross-browser in May 2026 when Firefox 151 shipped them, and querying a container by name alone became cross-browser the same month with Chrome 148.

My position: “responsive” should describe components, not pages. A component that only knows the viewport width is guessing about its own space. Use media queries for the page and for people; use container queries for everything you reuse.

What each one can see

Question Media query Container query
How wide is the viewport? Yes No
How wide is the space this component sits in? No Yes
Does the user prefer dark mode or reduced motion? Yes No
Can the device hover, and is the pointer coarse? Yes No
Is the page being printed? Yes No
Does an ancestor set --variant: featured? No Yes, with a style query
Can I size text relative to the component? No (viewport units only) Yes, with cqi units

The two do not compete. They answer different questions, and most interfaces need both.

Setting up a container

An element becomes a size container with container-type. Its descendants can then query it. The element cannot query itself, which is the first rule people trip over.

.card-wrapper {
  container-type: inline-size;   /* query my width */
  container-name: card;          /* optional, but makes intent clear */
}

/* Shorthand: name / type */
.card-wrapper { container: card / inline-size; }

.card {
  display: grid;
  gap: 1rem;
}

@container card (width > 30rem) {
  .card {
    grid-template-columns: 12rem minmax(0, 1fr);   /* image beside text */
  }
}

Container queries use the same range syntax that media queries now support, so (width > 30rem) and (30rem <= width < 50rem) both work. Without a name, @container (width > 30rem) queries the nearest ancestor size container of any name.

inline-size vs size

  • container-type: inline-size applies containment on the inline axis only. You can query width (or inline size), and the container’s height still comes from its content. This is what you want almost every time.
  • container-type: size contains both axes, so you can also query height. The container’s height can no longer depend on its content, so it collapses to zero unless you give it an explicit height. Use it only for elements with a known block size, such as a full-height panel.

The zero-width trap

Inline-size containment means the container’s width cannot depend on its children, because the children’s styles depend on the container’s width. That breaks circular sizing, but it has a side effect. If the container sits somewhere that sizes elements to their content, it has no content width to report and collapses.

/* WRONG: a container inside a flex row with no basis shrinks to nothing */
.toolbar { display: flex; }
.toolbar .search-wrapper { container-type: inline-size; }   /* width: 0 */

/* RIGHT: give the container a size that does not come from its content */
.toolbar .search-wrapper {
  container-type: inline-size;
  flex: 1;                      /* take the available space */
}

The same happens inside inline-block elements, floats and fit-content grid tracks. Block-level elements in normal flow, grid items in fr tracks and flex items with a real flex value are all safe containers.

Container query units

Container queries come with units that measure the query container, just as vw measures the viewport.

Unit 1 unit equals
cqi 1% of the container’s inline size
cqb 1% of the container’s block size
cqw, cqh 1% of the container’s physical width or height
cqmin, cqmax The smaller or larger of cqi and cqb
.card__title {
  font-size: clamp(1rem, 0.6rem + 2.5cqi, 1.6rem);   /* scales with the card, not the screen */
}

Prefer cqi over cqw. It follows the writing mode, so the same component works in vertical text layouts, a topic covered in the logical properties guide. Keep a rem term in the clamp() so text still respects the user’s font-size setting. If no ancestor is a size container, these units fall back to the equivalent small viewport units, which is a safe default rather than an error.

Style queries: context without classes

Style queries test the computed value of a custom property on a container. Every element is a style container by default, so no container-type is needed.

.section--dark { --surface: dark; }

@container style(--surface: dark) {
  .callout {
    background: var(--gray-800);
    color: var(--gray-50);
  }
}

Now any callout placed anywhere inside a dark section adapts, without the template adding .callout--on-dark. Style queries shine for this kind of contextual theming and for component variants driven by a single property. Two limits apply: cross-browser support covers custom properties only, not standard properties like display, and a style query cannot style the element that sets the property, only its descendants.

Some teams still support Firefox ESR or older Safari releases for enterprise customers. For them, treat style queries as an enhancement and keep a class-based fallback for critical states until those versions age out.

The component-first responsive model

Here is the structure I recommend. It has three layers, and each uses the tool that can actually see the condition it cares about.

  1. Page layout: media queries or intrinsic Grid. The macro layout, such as whether the sidebar exists, depends on the viewport. Use @media, or better, an intrinsic grid like repeat(auto-fill, minmax(...)) that needs no breakpoint at all.
  2. Components: container queries. Every reusable component adapts to its container. Layout areas such as the main column, the sidebar and each grid cell are declared containers.
  3. People and devices: media queries. prefers-color-scheme, prefers-reduced-motion, prefers-contrast, (hover: hover), (pointer: coarse) and print stay in media queries, because they describe the user, not the space.
/* Layer 1: page */
.page {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}
@media (width >= 64rem) {
  .page { grid-template-columns: 18rem minmax(0, 1fr); }
}
.page > main,
.page > aside { container-type: inline-size; }   /* every area is a container */

/* Layer 2: component */
.product { display: grid; gap: 1rem; }
@container (width > 32rem) {
  .product { grid-template-columns: 14rem minmax(0, 1fr); }
}
@container (width > 52rem) {
  .product { grid-template-columns: 18rem minmax(0, 1fr) 14rem; }  /* add a details column */
}

/* Layer 3: people */
@media (prefers-reduced-motion: reduce) {
  .product * { transition: none; }
}
@media (hover: hover) {
  .product:hover { box-shadow: var(--shadow-raised); }
}

The product component now has zero knowledge of where it lives. Drop it into the sidebar and it stacks. Drop it into the main column and it goes side by side. Put it in a modal, a dashboard tile or a grid cell and it picks the right layout for that space.

Grid cells as containers

Container queries pair especially well with an intrinsic card grid. The grid decides how many columns fit; each card decides how to arrange itself in whatever width its track ends up with.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(18rem, 100%), 1fr));
  gap: 1rem;
}
.cards > li { container-type: inline-size; }

Combine this with the subgrid card pattern from the subgrid guide and you get cards that align with their row and adapt to their own width, with no JavaScript and no variant classes.

Choosing breakpoint values

Write container breakpoints in rem or em, not pixels. When a reader increases their default font size, text gets bigger but the container’s pixel width does not, so a pixel breakpoint keeps the wide layout while the text no longer fits. A rem breakpoint scales with the font, so the component switches to its narrow layout at the right moment. Choose the values from the content, by resizing the component until it looks wrong, not from a list of device widths.

Frequently Asked Questions

What is the difference between container queries and media queries?

Media queries test conditions about the viewport, device and user preferences, such as screen width, hover support or dark mode. Container queries test conditions about an ancestor element declared as a container, usually its width, so a component can adapt to the space it actually occupies.

Do container queries replace media queries?

No. Container queries replace media queries for component layout, but media queries are still the only way to respond to user preferences like reduced motion or color scheme, input capabilities like hover and pointer type, and print. Most projects use both.

Why is my container query not working?

The most common causes are querying the element that is itself the container, forgetting container-type on an ancestor, or a container that sizes to its content and collapses to zero width, such as an inline-block or a flex item without a flex basis.

Are container style queries supported in all browsers?

Style queries for custom properties became supported in all major browsers in May 2026, when Firefox 151 added them. Querying standard CSS properties with style() is not yet cross-browser, so stick to custom properties.

Make components responsible for themselves

The viewport is a poor proxy for a component’s space, and it gets worse the more places a component is reused. Declare your layout areas as containers, move component breakpoints into @container, and keep media queries for the page and the person. Container queries also help with internationalization when combined with logical properties.

Rule of thumb: if a component would need a modifier class to look right in a new place, it should have been a container query.

Originally posted on 11 June 2026 and post last modified on 11 June 2026

Container Queries CSS Front-End Development Media Queries Responsive Design

Share this article

Leave a Reply

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