You have a sidebar full of cards, and every one of them carries card--compact. The template loop adds the class, so nobody types it by hand. Fine. Then design looks at the page and says the compact cards further down shouldn't be compact, because the tight padding there isn't separating anything. It just looks inconsistent.

The class sits on the card. The reason for the class comes from where the card is on the page. Wrong place.

Kevin Powell's new CSS-Tricks article fixes this with container style queries, which the article says are now Baseline Newly Available. The move: stop putting the variant on the component. Put a setting on an ancestor, and let the component decide what to do with it.

Same at-rule, different question

A size query asks how wide the container is. A style query uses the same @container at-rule and asks what a custom property is set to:

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

Read it as: if the nearest container has --theme: high-contrast, style .card like this.

No setup required. Every element is a style container by default, so there's no container-type to declare. With size queries, a missing container-type is the usual reason nothing happens. Here that failure mode is gone.

Custom properties only, for now. The spec also covers regular properties, like style(display: flex), but the article says that isn't supported anywhere yet.

The rule that catches people is that an element can't query itself:

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

Same as size queries. The query looks at an ancestor, so the property has to be set somewhere above .card. Powell notes that if() is on the way and can read custom properties on the element it's used on. The trade-off is that if() covers a single property, while a style query block holds as many declarations as you need.

From modifier to setting

Here's a density modifier the familiar way, as an attribute:

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

And the style query version from the article:

[data-card-layout="alternative"] { --card-layout: alternative; } .card { @container style(--card-layout: alternative) { padding: .5rem; gap: .5rem; } } 

More code. Yes. Powell admits that if you think in modifiers, it looks like extra work for nothing. The payoff is upstream. The alternative styles are written once, inside .card, and anything that can set --card-layout can switch them on: a media query, a container query, a :has() selector, a parent section. A CMS option that writes one custom property onto a section of the page works too.

That's the sidebar fix. In the article's version, a @media (width > 700px) block sets up the two-column layout and gives .sidebar its compact density through a custom property. The card markup doesn't change. The decision moves to the part of the page that actually has the context.

The overflow grid that should scroll down

The better demo is the overflow grid. Powell's base pattern:

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

It scrolls sideways. Drop it into a hero that splits into two columns on larger screens and sideways scrolling in that narrow column gets awkward. He wants it to scroll vertically there instead.

First, make the flow direction a setting with a default of column:

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

Then the split flips the setting when it goes to two columns:

.split { @container main (width > 40rem) { --overflow-grid-direction: row; grid-template-columns: repeat(2, 1fr); } } 

On its own, that looks bad. The grid is still sized for horizontal scrolling, so you get a tall column and no overflow at all. Flipping grid-auto-flow changed the direction. None of the sizing came along.

So the grid listens for its own setting:

.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 */ } } 

Look at who knows what. .split never selects .overflow-grid. It sets one custom property. The grid owns every rule about how it looks in each direction. Custom properties inherit, so the same value feeds the var() in grid-auto-flow and the style query, and because it's set on .split, an ancestor, the self-query rule never gets in the way.

Styling a child from its layout works. Powell calls it awkward and says it leads to fragile code bases. I agree, and this example makes the case better than any argument about selector coupling I've read.

Modifier classes stay

Powell is clear that this doesn't retire modifier classes or data attributes. If a button just needs to be the accent version or a pill, a modifier is the right tool. It also styles the element it sits on, which a style query can't. And sometimes a plain media query, a container query or an intrinsic layout does the job without either.

His test for when style queries pay off: the condition and the styles live in different places, or one condition has to change several things at once.

Try it on your next layout

Find a component you style from its parent. A selector like .split .overflow-grid gives it away. Turn whatever the parent is changing into a custom property with a default. Move the rules into a @container style() block inside the component. Have the parent set the property. Keep it on an ancestor.

The missing piece is querying real properties. style(display: flex) is in the spec, and no browser supports it yet. Until then, custom properties are the switch.