Style Queries as Conditional Modifiers
This article is adapted from a lesson in Writing Resilient CSS, the newest module in Kevin’s course, CSS Demystified. The module covers configuration options with custom properties, math functions, relative colors, :has(), style queries, and more, all put to work in practical, reusable patterns. The course is 32% off until December 2, 2026.
Modifiers have been with us for a long time, often with modifier classes, but also with attribute selectors. Some common examples are things like different versions of alerts and buttons.
/* BEM approach */
.button–accent {
background-color: var(–clr-accent);
}
/* CUBECSS approach */
.toast[data-type=”error”] {
border-color: var(–clr-warning);
}
For a lot of use cases, either of those approaches work great, but when it comes to layout, a lot of the time we want the context of where the element is being used to control things.
A card is a nice example of this, where we might have several versions.
.card {
display: grid;
gap: .75rem;
}
.card–horizontal {
grid-template-columns: 1fr 2fr;
}
.card–compact {
padding: .75rem;
gap: .5rem;
}
The issue here is, if I’m using .card–compact, I probably want to do that for all the cards within a section. That can be annoying to apply to each card, but that’s rendered moot since most people are doing some sort of looping in a template like this these days.
{relatedPosts.map((post) => (
{post.title}
{post.excerpt}
))}
And this works great, as long as we always want the cards to be compact.
However, you might put this together and send it off to the design team for approval, only for them not to like that when the compact cards are lower down the page, the smaller padding isn’t needed to create visual separation between the two sections. Instead of helping with the hierarchy, it’s simply creating a strange visual inconsistency.
We could use something like a resize observer to try and deal with this, but we have a better tool at our disposal: Container Style Queries, which are now Baseline Newly Available.
While they all start with container, for the rest of this article I interchangeable use container query or size query for a container size query, and style query for what we’re talking about here.
A quick look at style queries
Before we get into the good stuff, a quick look at the basics of style queries.
You’re probably familiar with size queries, which work similar to @media but look at the size of a container, rather than the viewport. Container style queries use the same @container at-rule as container size queries, but instead of looking at a size, they look at the value of a custom property.
@container style(–theme: high-contrast) {
.card {
background: white;
color: black;
border-color: black;
}
}
This reads as, “if the nearest container has –theme: high-contrast, style the .card like this.”
A couple of important notes about them:
- You don’t need to set up a container. Unlike size queries, every element is a style container by default, so there’s no container-type to declare.
- They only work with custom properties for now. Querying regular properties, like style(display: flex), is in the spec but isn’t supported anywhere yet.
Elements can’t query themselves
Just like with size queries, style queries look at an ancestor, not the element itself. That means this won’t work:
.card {
–card-layout: alternative;
}
@container style(–card-layout: alternative) {
.card {
grid-template-columns: 1fr 2fr; /* doesn’t work */
}
}
The custom property needs to be set on something above the card. Sometimes this can be annoying, and it always tends to catch people out, but because of how I use them, I don’t find this to be much of a limitation.
if() is also on the way, and allows for style queries as well, but if() is able to see the value of custom properties on the element it’s being used. The big difference is it can only be used for a single property, while style queries can be used for rules with multiple properties.
From a modifier to a style query
Let’s take a look at a card again, first with a modifier like we might do now. To change it up, we’ll use an attribute modifier instead of a class.
[data-card-density=”compact”] {
padding: .5rem;
gap: .5rem;
}
Switching this up to use a style query, we could do something like this instead:
[data-card-layout=”alternative”] {
–card-layout: alternative;
}
.card {
@container style(–card-layout: alternative) {
padding: .5rem;
gap: .5rem;
}
}
If we’re in the mindset of how we’re used to using modifiers, this feels like a lot more work with no real payoff, but now, anything that can set a custom property can switch it: a media query, a container query, a :has() selector, or a parent section. The styles for the alternative layout are written once, and there are lots of ways to switch them on, making it so much more versatile.
One easy win here is if you’re working with a CMS. Now, the author of the page can chose from an option, which just sets one custom property value for a section of the page, and the appropriate styles kick in.
Where I feel like this has the most potential is when dealing with conditional styles like the sidebar we looked at earlier, where we can enable the compact card layout however we want:
.layout {
display: grid;
}
@media (width > 700px) {
.layout {
grid-template-columns: 1fr 18rem;
.sidebar {
–card-density: compact;
}
}
}
CodePen Embed Fallback
We can do so much more
Here’s an overflow grid pattern I use quite a bit:
.overflow-grid {
display: grid;
gap: 1rem;
grid-auto-flow: column;
grid-auto-columns: max(40%, 25ch);
padding-block-end: 1rem;
overflow: auto;
}
The problem is that it always overflows, even if I don’t have enough children to warrant it. Like here, the first row, the third card is just barely overflowing, making it awkward more than anything.
One solution is to use a container query and :has(), where we let the items fill the space when there are fewer than five of them, and there’s enough space:
.overflow-grid {
/* … */
/* fewer than 5 children */
&:not(:has(> :nth-child(5))) {
@container (width > 75ch) {
grid-auto-columns: 1fr;
}
}
}
CodePen Embed Fallback
And this works really well! But sometimes, we might need to make some bigger changes depending on where the elements are being used.
Let’s say, for example, our page has a hero component that goes to two columns on larger screens, with a heading on one side and a set of featured articles in an overflow grid on the other. The container query is making sure the overflow is on in the narrow space, but horizontal scrolling there seems a little awkward.
CodePen Embed Fallback
When the split happens, I’d rather the grid scrolled vertically instead, and we can leverage style queries to pull this off.
First, we give the overflow grid a configuration option for its direction, defaulting to columns:
.overflow-grid {
grid-auto-flow: var(–overflow-grid-direction, column);
}
Now the split can change when it goes to two columns:
.split {
@container main (width > 40rem) {
–overflow-grid-direction: row;
grid-template-columns: repeat(2, 1fr);
}
}
And this works, but the result isn’t great. The grid is still sized for horizontal scrolling, so we end up with a tall, awkward column, and no overflow at all.
CodePen Embed Fallback
This is where style queries come in. The overflow grid can “listen” for the direction change and adjust its own styles to match.
.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 */
}
}
The split section isn’t styling the overflow grid directly, which is possible, but always awkard, and leads to fragile code bases. Instead, we created some configurable settings on that grid, and simply adjusted them as needed.
CodePen Embed Fallback
They aren’t a replacement for modifiers
I don’t think style queries mean it’s time to throw out modifier classes (or data modifiers). They still work great, especially since a style query can’t look at the element it’s styling. If I simply want my button to be an accent button, or a pill shape, then a modifier makes a lot of sense.
On top of that, a regular media query or container query, or even an intrinsic pattern might get the job done, and that’s great too.
Wrapping up
Where style queries shine is when the condition and the styles need to live in different places, or when one condition needs to affect several things at once, like the split above. Instead of a layout reaching in and styling its children, it changes a few settings, and each component decides how to respond.
That’s a big part of what I think of as writing resilient CSS: components that keep working, and adapt sensibly, no matter where they end up.
This article is adapted from a lesson in Writing Resilient CSS, the newest module in my course, CSS Demystified. The module covers configuration options with custom properties, math functions, relative colors, :has(), style queries, and more, all put to work in practical, reusable patterns. The course is 32% off until December 2, 2026.






