Picture a product page with a main column of large cards and a narrow sidebar of related items. The sidebar cards need to be smaller and tighter, so you add a “compact” class to each one. Then the design team asks for the compact look only when the sidebar sits beside the main column, not when it drops below it on a phone. Suddenly a simple class has become a headache.
This is exactly the sort of problem that container style queries were made for, and they are now supported in all the major browsers. Rather than tagging every component with a modifier, you set one custom property higher up the page and let each component decide how to respond.
- In brief: style queries let a component react to the value of a custom property set on one of its ancestors.
- No
container-typeis needed: every element can act as a style container. - For now they only work with custom properties, not ordinary CSS properties.
- They work best when the thing that decides and the thing being styled live in different places.

Style queries in a nutshell
The syntax
If you have used container size queries, the at-rule will look familiar. The difference is what goes inside the brackets: instead of a width, you check whether a custom property holds a particular value.
@container style(--mode: dark) {
.banner {
background: #111;
color: #f5f5f5;
}
}
In plain English: if the nearest ancestor has --mode: dark, give the banner a dark background and light text.
Two things that catch people out
First, an element cannot query itself. Setting --mode: dark on .banner and then querying it inside the same element does nothing; the property has to be set on a parent or another ancestor. Second, only custom properties can be checked today. Querying something like style(display: grid) is described in the specification, but no browser supports it yet.
The upcoming if() function can read custom properties on the element itself, but it only sets one property at a time. A style query can switch a whole block of declarations at once.
Before and after: switching cards to compact mode
Before: a modifier on every card
The traditional approach is a modifier class or data attribute that sits on each component:
.product-card {
display: grid;
gap: 1rem;
padding: 1.5rem;
}
.product-card.is-small {
gap: .5rem;
padding: .75rem;
}
That works, but the decision is baked into the markup. If you only want small cards at certain screen sizes or in certain sections, you end up juggling classes in your templates or reaching for JavaScript.
After: one setting on the parent
With a style query, the card simply listens for a setting:
.product-card {
display: grid;
gap: 1rem;
padding: 1.5rem;
@container style(--card-size: small) {
gap: .5rem;
padding: .75rem;
}
}
@media (width > 60rem) {
.related-items {
--card-size: small;
}
}
The compact styles are written once, inside the card. What switches them on can be anything that sets a custom property: a media query, a container size query, a :has() selector or a class on a page section. On a content-managed site, an editor could pick “small cards” from a block setting, and all it needs to do is output that single property.

Going further: a grid that changes direction
The real payoff comes when one decision needs to affect several things. Take a row of cards that scrolls sideways on small screens. Place it inside a two-column hero on a wide screen and sideways scrolling in a narrow column feels awkward; vertical scrolling would suit it better. The hero can expose a direction setting, and the scrolling list adjusts its own sizing to match:
.scroll-row {
display: grid;
grid-auto-flow: var(--scroll-direction, column);
grid-auto-columns: minmax(16rem, 40%);
overflow: auto;
@container style(--scroll-direction: row) {
grid-template-columns: 1fr;
max-block-size: 60vh;
}
}
.hero {
@container (width > 45rem) {
grid-template-columns: 1fr 1fr;
--scroll-direction: row;
}
}
Notice that the hero never styles the list directly. It changes a setting, and the list handles the rest. That separation keeps components self-contained and far less likely to break when they are moved around a site.
Should you ditch modifier classes?
No. If a button should always be the accent colour, or always have rounded ends, a modifier class is still the simplest, clearest choice, especially because a style query cannot look at the element it is styling. Plain media queries, size queries and naturally flexible layouts also remain perfectly good answers to many problems.
Style queries earn their place when the condition and the styling belong in different parts of your code, or when a single change should ripple through several components. For small business sites, that translates into page sections that adapt neatly wherever they are placed, fewer one-off fixes and templates that are easier for editors to work with.
So next time you find yourself adding the same modifier to a dozen components, ask yourself: could one custom property on the parent do the job instead?











