Container query

A container query applies CSS based on the size of a parent container, not the whole screen, so a component adapts to wherever it is placed.
Design
Created on
23.08.2026

Summarize this

A container query applies styles based on the size of a parent container rather than the size of the whole viewport. It lets a component adapt to the space it actually sits in, so the same card looks right in a wide main column and in a narrow sidebar. It is the piece responsive design was missing for true component-based layouts.

How a container query works

You mark an element as a query container, then write rules against its width.

  • Declare the container: .card-wrap { container-type: inline-size; }
  • Query it: @container (min-width: 400px) { .card { display: flex; } }
  • Optionally name it with container-name to target a specific ancestor.

Container query versus media query

Both apply conditional styles, but they measure different things.

AspectMedia queryContainer query
MeasuresThe viewport (whole screen)A parent container
Best forPage-level layoutReusable components
ReusabilityTied to screen sizeWorks anywhere it is placed

Container query units

Alongside the rules, container query units size against the container: cqw (1% of container width), cqh (height), and cqi / cqb (inline and block size). They let type and spacing scale with the component instead of the screen.

Why it matters for design systems

In a component-based system, the same card, list or media object is dropped into many layouts. Media queries cannot know how wide that slot is; container queries can. This makes components truly self-contained and is why design systems adopted them quickly.

Browser support and best practices

Container queries are supported across all modern browsers. Declare the container on a wrapper rather than the component itself so the component stays reusable, and keep viewport media queries for page-level structure while using container queries for the pieces inside.

Container queries at BeBranded

At BeBranded we build components that adapt to their slot with container queries, so a card behaves correctly wherever it lands. It is part of the systematic, component-driven front-end in our design work.

FAQ

A container query applies CSS based on the size of a parent container instead of the viewport, so a component adapts to the space it sits in rather than the whole screen.
A media query responds to the viewport size, while a container query responds to a parent container. Media queries suit page layout; container queries suit reusable components.
Set container-type: inline-size on a wrapper, then write @container (min-width: 400px) { ... }. The rules apply based on that container's width, not the screen.
Units like cqw, cqh, cqi and cqb size relative to the container: cqw is 1% of its width. They let type and spacing scale with the component instead of the viewport.
Yes. Container queries are supported across all modern browsers, so they are safe to use on current projects, with a graceful fallback for very old ones.
Use a container query for a reusable component that can appear in different widths, and a media query for the overall page layout. They complement each other.

Ready to boost your conversions?

Our team is here to understand your needs & work with you to create your next projects.
Get news, infos and resources.
Actionable tips delivered straight to your inbox.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.