What container style queries actually do

Container style queries reuse the @container at-rule familiar from size queries, but instead of evaluating a container's dimensions they evaluate the value of a custom property. The pattern reads as: if the nearest container carries --theme: high-contrast, apply the enclosed styles to .card.

@container style(--theme: high-contrast) {
  .card {
    background: white;
    color: black;
    border-color: black;
  }
}

Two constraints define the feature today. No container setup is required — every element is a style container by default, so there is no container-type to declare. And only custom properties can be queried; regular declarations such as style(display: flex) are specified but unimplemented everywhere.

Style queries are now Baseline Newly Available. Throughout, the term style query refers to this feature, while size query covers the container size query.

The ancestor requirement

As with size queries, the element being styled cannot be the element being queried. The custom property must be set on something above the card:

.card {
  --card-layout: alternative;
}

@container style(--card-layout: alternative) {
  .card {
    grid-template-columns: 1fr 2fr; /* doesn't work */
  }
}

This catches people out, but in practice it is rarely limiting. Note also that if() is coming and can perform style queries while reading custom property values on the element itself; its limitation is scope — one property per condition, whereas style queries can carry rules with many properties.

Modifiers, and where they stop scaling

Modifier classes and attribute selectors such as [data-variant] handle versioning of alerts and buttons well. Layout is different: there, the surrounding context usually should decide. Consider a card with several variants, applied through an attribute modifier:

[data-card-density="compact"] {
  padding: .5rem;
  gap: .5rem;
}

Rewriting it as a style query:

[data-card-layout="alternative"] {
  --card-layout: alternative;
}

.card {
  @container style(--card-layout: alternative) {
    padding: .5rem;
    gap: .5rem;
  }
}

To anyone used to modifiers this looks like extra machinery for no gain. The gain is that anything able to set a custom property becomes a switch — a media query, a container query, a :has() selector, a parent section. The alternative layout is written once and can be enabled from many places. In a CMS, an author picks an option that sets a single property on a page section and the right styles follow. Conditional contexts are where it pays off most, letting a compact card layout be turned on however is convenient:

.layout {
  display: grid;
}

@media (width > 700px) {
  .layout {
    grid-template-columns: 1fr 18rem;

    .sidebar {
      --card-density: compact;
    }
  }
}

A template loop, incidentally, makes the manual annoyance of stamping .card--compact on every card moot — but not the problem that compactness may be wrong for a given placement.

A worked case: overflow grids in a split layout

This overflow grid pattern is a common one:

.overflow-grid {
  display: grid;
  gap: 1rem;
  grid-auto-flow: column;
  grid-auto-columns: max(40%, 25ch);
  padding-block-end: 1rem;
  overflow: auto;
}

Its flaw: it always overflows, even when there aren't enough children to justify it. Below, the third card in the first row spills just barely, which reads as awkward rather than intentional.

Two rows of three article cards that overflow the horizontal space, creating scrollbars.

A container query combined with :has() fixes the simple case, letting items fill the space when fewer than five exist and room is available:

.overflow-grid {
  /* ... */

  /* fewer than 5 children */
  &:not(:has(> :nth-child(5))) {
    @container (width > 75ch) {
      grid-auto-columns: 1fr;
    }
  }
}

Now the harder scenario. A hero component goes two-column on large screens, heading on one side, featured articles in an overflow grid on the other. The container query keeps overflow on because the space is narrow, but horizontal scrolling there feels wrong; a vertical scroll is wanted once the split happens. To get there, the grid first gains a configuration option for direction, defaulting to columns:

.overflow-grid {
  grid-auto-flow: var(--overflow-grid-direction, column);
}

The split then changes direction when it goes two-column:

.split {
  @container main (width > 40rem) {
    --overflow-grid-direction: row;

    grid-template-columns: repeat(2, 1fr);
  }
}

The result is poor: the grid is still sized for horizontal scrolling, leaving a tall, awkward column with no overflow at all. So the overflow grid listens for the direction change and adjusts its own styles:

.overflow-grid {
  @container style(--overflow-grid-direction: row) {
    grid-template-columns: 1fr; /* a single, full-width column */
    max-block-size: 50vb; /* keep the overflow, but vertically */
  }
}

Crucially, the split section never styles the grid directly — which is possible and leads to fragile code. It adjusts configuration settings; the grid responds. Where these shine is when the condition and the styles must live in different places, or when one condition has to affect several things at once. A layout changes a few settings and each component decides how to respond.

Not a replacement

Modifier classes and data modifiers still earn their place — a style query can't target the element it styles, so "accent button" or "pill shape" is naturally a modifier. A plain media query, a container query, or an intrinsic pattern will often do the job too.

What drew this redesign out was the design team's verdict on compact cards lower down the page: the reduced padding wasn't needed purely to separate sections and instead of reinforcing hierarchy it produced a visual inconsistency. A resize observer could chase that; a style query expresses it directly.

What to change

Modifiers served alerts, buttons, and cards well for years; layout context is the case they fit worst, because the decision belongs somewhere the element can't see. Style queries move that decision to an ancestor and make the result swappable: one property upstream, a few reusable configuration settings downstream, components that adapt wherever they land.