What are CSS container queries?

IntermediateCSS interview questionBy , updated 17 November 2026

Short answer

CSS container queries let you style an element based on the size of an ancestor container instead of the viewport. You mark an element as a container with container-type: inline-size, then write @container (width >= 30rem) { ... } rules for its descendants. A component can then adapt to the space it is given, wherever it is placed, which media queries cannot do.

The problem they solve

A media query asks about the viewport: "is the window at least 800px wide?" That works for page layout, but a component does not live in the window. The same product card might sit in a wide main column, a narrow sidebar and a three-column grid on one page. At a 1,200px viewport, the sidebar card may be only 280px wide, yet every media query says "wide screen". You end up writing modifier classes like .card--in-sidebar and keeping them in sync with the layout.

A container query asks the question the card actually cares about: "how much room do I have?" The card's CSS no longer depends on where it is used.

How it works

There are two steps. First, make an ancestor a query container:

.card-slot {
  container-type: inline-size; /* query its width */
  container-name: card;        /* optional, lets rules target it by name */
}

/* Shorthand for the two lines above */
.card-slot { container: card / inline-size; }

Then write @container rules. They apply to descendants of the container when the condition is true:

@container card (width >= 30rem) {
  .card { grid-template-columns: 10rem 1fr; }
}

The browser looks up the nearest ancestor that is a query container (with the matching name, if you gave one) and tests its size. Without a name, the rule uses the nearest container of any name.

The values of container-type

Value What you can query Catch
inline-size Width in horizontal writing modes The container's width can no longer come from its children
size Width and height Its height can no longer come from its children either, so it needs an explicit height
normal (default) No size queries Still works for style queries

That catch is the important detail. To avoid a loop (child styles change the container's size, which changes the query, which changes the child styles), the browser applies size containment: it lays out the container as if it had no content in the queried axis. A block element in normal flow gets its width from its parent, so inline-size costs nothing there. A shrink-to-fit element, such as an inline-block, a float, or a flex item with no set width, collapses to zero width. Put the container on a wrapper that already has a width, not on the component itself.

A card example

<div class="card-slot">
  <article class="card">
    <img src="/lamp.jpg" alt="Brass desk lamp" />
    <div>
      <h2>Brass desk lamp</h2>
      <p>Adjustable arm, warm LED, two-year warranty.</p>
    </div>
  </article>
</div>
.card-slot { container: card / inline-size; }

.card { display: grid; gap: 1rem; }            /* narrow: image on top */
.card img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
.card h2 { font-size: clamp(1rem, 5cqi, 1.5rem); }

@container card (width >= 28rem) {            /* wide: image beside text */
  .card { grid-template-columns: 12rem 1fr; align-items: start; }
}

Drop .card-slot into a sidebar and the card stacks. Put it in the main column and the image moves beside the text. No class changes and no knowledge of the page layout.

Note that .card-slot is a separate wrapper. An element cannot query itself: rules inside @container style the container's descendants, never the container.

Container query units

Container query units are lengths relative to the nearest query container:

  • cqi and cqb: 1% of the container's inline or block size.
  • cqw and cqh: 1% of its width or height.
  • cqmin and cqmax: the smaller or larger of cqi and cqb.

In the example, 5cqi makes the heading scale with the card, and clamp() keeps it readable at the extremes. If no ancestor is a suitable container, the units fall back to the small viewport units (svw, svh), so they never break, they just stop being component-relative. Use cqi rather than cqw if you support vertical writing modes.

Container queries vs media queries

Media queries Container queries
Asks about Viewport, device and user preferences An ancestor container's size (or styles)
Best for Page layout, prefers-color-scheme, prefers-reduced-motion, print Reusable components placed in different slots
Needs setup No Yes, container-type on an ancestor
Units vw, vh, svh, dvh cqi, cqb, cqmin, cqmax

They work together. Media queries decide the page grid; container queries let each component react to the column it lands in. User preference queries such as reduced motion stay as media queries, because they are about the user, not the space. For the page-level side, see mobile-first responsive design with media queries and responsive vs adaptive design.

Size container queries have worked in all current major browsers since 2023, so they are safe for production use today.

Practise it

Common follow-up questions

Why did my container collapse to zero width?

Size containment means the container's width cannot depend on its content. If the element is shrink-to-fit (an inline-block, a float, an absolutely positioned box, or a flex item without a basis), it has no other source of width and becomes 0px wide. Give it a width, or move container-type to a block-level wrapper.

What are style queries?

@container style(--layout: compact) { ... } tests a custom property's computed value on the container instead of its size. Every element is a style container by default, so no container-type is needed. Support for custom-property style queries arrived later than size queries and is not in every browser, so check current support and treat it as an enhancement.

Can I nest containers?

Yes. An unnamed @container rule uses the nearest container. Name containers (container-name: sidebar, container-name: card) when an inner component needs to query an outer one, otherwise it always sees the closest.

Do container queries hurt performance?

Not in normal use. Containment actually helps the browser, because it knows the container's size cannot depend on its children. Avoid making hundreds of elements containers for no reason, but a container per card is fine.

Can I use them with Tailwind or CSS Modules?

Yes. They are plain CSS, so they work in CSS Modules and Sass files. Tailwind v4 includes @container and size variants such as @md: out of the box.

Mistakes to avoid

  • Putting container-type on the element you want to restyle

    It must be an ancestor.

  • Using container-type: size without giving the container a height, which collapses it vertically

  • Making a shrink-to-fit element a container and wondering why it disappears

  • Replacing every media query

    Page layout and user preferences still belong in @media.

  • Forgetting a name when components nest, so an inner rule queries the wrong container

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Switch a card's layout with CSS container queries and prove it by measuring three container widthsEasy25 min
  2. Build a holy-grail layout with CSS Grid that stacks in narrow containersEasy20 min
  3. Turn a data table into stacked cards on narrow screens without losing the column labelsEasy25 min
  4. Build a three-column social feed layout with sticky sidebars using CSS gridEasy20 min
  5. Build a responsive navigation that collapses into a details menu on small screensEasy25 min