Responsive Components with CSS Style Queries
Adapt styles based on actual styles—not just size
Most responsive design decisions come down to size.
Viewport width, container width, breakpoints—everything is tied to how big something is.
But not every design decision is about size.
Sometimes you want a component to react to how it is styled. A theme change. A variation. A parent deciding something about its children.
CSS style queries are designed for that.
Why size-based thinking has limits
Container queries were a big step forward because they let components respond to their own space instead of the viewport.
But even that only solves part of the problem.
Not every variation in a component is driven by size. Sometimes the difference comes from context—like a theme, a variant, or a design decision made higher up in the component tree.
In those cases, we often fall back to extra classes, data attributes, or JavaScript just to communicate that state.
That works—but it is not especially elegant.
What CSS style queries actually do
Style queries let you apply styles based on the computed styles of an element—rather than its size.
In practice, that means a component can react to values like custom properties or other style-driven signals.
@container style(--theme: dark) {
.card {
background: #111;
color: white;
}
}
Here, the component is not responding to width or height. It is responding to a styling decision: the value of --theme.
That is a different kind of responsiveness—one based on intent instead of dimensions.
The mental model (this is the shift)
It helps to think of style queries as “listening” to styles.
Instead of pushing classes down into components, you define values—often with custom properties—and let components respond to them.
.container {
container-type: style;
--variant: compact;
}
@container style(--variant: compact) {
.item {
padding: 0.5rem;
}
}The component is not asking “how big am I?” It is asking:
“What kind of environment am I in?”
That distinction is subtle, but it changes how you structure your CSS.
Where this starts to feel useful
Theme-aware components
One of the most natural uses is theming.
Instead of scattering theme classes throughout your markup, you can define a theme at a higher level and let components adapt.
.app {
container-type: style;
--theme: dark;
}
@container style(--theme: dark) {
.card {
background: #111;
color: white;
}
}The component reacts without needing to know where the decision came from.
Component variants without extra classes
Variants often rely on modifier classes like .card--compact or .button--large.
Style queries offer another approach—one where the variation is expressed as a value rather than a class.
.list {
container-type: style;
--density: compact;
}
@container style(--density: compact) {
.item {
gap: 0.5rem;
}
}This can make systems feel more flexible, especially as variations grow.
Context-driven styling
Sometimes a component needs to adapt based on where it lives.
Instead of writing deeply nested selectors, you can define context at the container level and let the component respond.
.sidebar {
container-type: style;
--layout: dense;
}
@container style(--layout: dense) {
.nav-item {
padding: 0.25rem 0.5rem;
}
}The relationship becomes explicit, rather than implied through selector complexity.
How this differs from container queries
Container queries answer questions about size. Style queries answer questions about state.
They are not competing features—they are complementary.
You might use a container query to adjust layout when space changes, and a style query to adjust appearance when context changes.
One responds to dimensions. The other responds to decisions.
What to watch for
The biggest thing to keep in mind is that style queries depend on container context. You need to explicitly define a style container using container-type: style.
It is also worth remembering that this feature is still evolving. Support may be limited, and syntax or behavior could change as the spec matures.
And while this can reduce the need for extra classes, it does not eliminate the need for clear structure. Overusing style queries can make relationships harder to trace if you are not careful.
Use style queries when a component should react to context—not just size.
Can you use this today?
This is still an experimental feature, so you should treat it as progressive enhancement.
That means building a solid baseline first, then layering style queries on top where supported.
If you are already using custom properties for theming or variants, you are in a good position to adopt this when support improves.
Quick answers
Is this a replacement for classes?
Not entirely. It is another tool—one that can reduce the need for classes in some situations.
Do I have to use custom properties?
They are the most common use case, because they are easy to query and propagate.
What is the biggest benefit?
Components can respond to context without being tightly coupled to markup structure.
Want to go deeper?
As CSS becomes more expressive, features like style queries point toward a more declarative way of building interfaces—one where components respond to signals instead of relying on rigid structure.
Size was the first step. Context is what comes next.