Stop Toggling Theme Classes: CSS Container Style Queries React to Custom Properties
October 13, 2026 · 8 min read · CSS
By Syed · Frontend Engineer & Creator of UITweak
Every design system eventually grows a variant problem. The card needs a dark version in the footer, a compact version in the sidebar, and the button inside the card needs to know which background it sits on. So you add .card--dark, then .card--compact, then a dark compact card, and the theme class has to be threaded through three components by JavaScript that must stay in sync with the DOM. Container size queries solved half of this in 2023: components reacting to their width. The other half, reacting to the context they sit in, became Baseline in May 2026 with style queries.
30-second version: @container style(--theme: dark) styles descendants based on a custom property set on an ancestor container. Set --theme: dark on a section, and every card and button inside it adapts, no JS class toggles, no nesting bugs. Supported in Chrome 111+, Safari 18+ and Firefox 151+.
Why data-attribute theming breaks when nested
The common CSS-only theming pattern looks like this:
[data-theme="dark"] .card {
background: #14181f;
color: #e8ecf2;
}
[data-theme="light"] .card {
background: #ffffff;
color: #1a1d23;
}Works fine until contexts nest. Put a light panel inside a dark section:
<section data-theme="dark">
<div class="card">dark, correct</div>
<aside data-theme="light">
<div class="card">should be light...</div>
</aside>
</section>The inner card matches both selectors. Both have identical specificity, so the stylesheet's source order decides, not the DOM nesting. Whichever rule comes last wins everywhere, and the inner context cannot reliably override the outer one. Every nested theme becomes a specificity fight, and the usual escape is JavaScript toggling classes, which has to stay in sync with state that CSS already knew.
The style query recipe
Style queries read a custom property from the nearest ancestor that is a container. Two pieces are required: the ancestor declares itself a container with container-type: style, and descendants query it:
.theme-scope {
container-type: style;
--theme: light;
}
.theme-scope.dark {
--theme: dark;
}
@container style(--theme: dark) {
.card {
background: #14181f;
color: #e8ecf2;
}
}
@container style(--theme: light) {
.card {
background: #ffffff;
color: #1a1d23;
}
}Now the inner context wins by proximity, not source order. A card reads --theme from the closest ancestor container, so a light panel inside a dark section just works. No specificity tricks, no JavaScript. Flip the property on the ancestor and every descendant re-evaluates instantly.
One thing to note: the ancestor must have container-type: style. Without it there is no container to read from, and the query will not match. This is the single most common reason a style query silently does nothing.
Example: a density toggle with zero JavaScript
Theming is the headline use, but density is where style queries quietly shine. Two regions of the same page can carry different spacing without any component knowing which region it is in:
.features {
container-type: style;
--density: spacious;
}
.bugs {
container-type: style;
--density: compact;
}
@container style(--density: compact) {
.card { padding: 8px; gap: 6px; }
.card h3 { font-size: 0.9rem; }
}
@container style(--density: spacious) {
.card { padding: 24px; gap: 16px; }
.card h3 { font-size: 1.25rem; }
}The cards are identical markup. The context decides the treatment. Add a third density later and no component file changes, only a new query block.
Example: theme sections on one page
A landing page with a dark footer band and a light hero is the classic case. Mark each section as a container, set one property, and let every button, badge and card inside react:
.page-section {
container-type: style;
}
.page-section--dark { --theme: dark; }
.page-section--light { --theme: light; }
@container style(--theme: dark) {
.btn {
background: #e8ecf2;
color: #14181f;
border-color: transparent;
}
.badge { background: rgb(255 255 255 / 0.12); }
}
@container style(--theme: light) {
.btn {
background: #14181f;
color: #e8ecf2;
}
}The button never learns about themes. It only knows its default look plus two contextual overrides. Move a component from the hero to the footer and it restyles itself. This is what context-independent components actually look like.
The fallback: @supports and a sane default
Style queries are Baseline since May 2026, but older browsers ignore the whole @container style() block. Write the default look first, then layer the queries inside a feature check:
.card {
/* default: light, works everywhere */
background: #ffffff;
color: #1a1d23;
}
@supports (container-type: style) {
@container style(--theme: dark) {
.card {
background: #14181f;
color: #e8ecf2;
}
}
}Unsupported browsers get the default theme, which is exactly what they would have rendered before. If the dark variant is load-bearing rather than decorative, keep the data-attribute selectors as the fallback layer instead, they still work, they just have the nesting limitation.
Build the button in the studio, then make it contextual
The workflow that ties it together: define your design tokens first in uitweak's Tailwind config visualizer, then build the button's full variant set (sizes, states, radius) in uitweak's Tailwind button studio. Copy the generated button CSS as your default look, then add one @container style(--theme: dark) block for the dark context. The studio handles the craft of the button, the style query handles where it lives, and no theme prop ever gets drilled through your components.
Gotchas before you ship
1. Custom properties only
In every engine, style() can only query custom properties. @container style(display: flex) does nothing. If you need to branch on a regular property, you are still out of luck, and the query will silently not match.
2. Equality matching only
Style queries compare exact values: style(--density: compact). There is no range syntax for style queries in any shipped browser, so you cannot write "denser than compact". Design your tokens as a small set of named values instead of a numeric scale.
3. The container must opt in
Only ancestors with container-type: style are visible to style queries. Setting the custom property alone is not enough. This also means you can have size containers and style containers coexist: different containment for different queries, no conflict.
Browser support
Container style queries reached Baseline on May 19, 2026, when Firefox 151 shipped them and completed the set.
- Chrome / Edge: 111+
- Safari: 18+
- Firefox: 151+
Use the @supports (container-type: style) pattern above for older browsers. The default theme you write outside the query is the graceful degradation.
The one-way rule: a container cannot query itself
This is the limitation that confuses everyone exactly once. A style query reads custom properties from ancestor containers, never from the element itself. Writing @container style(--theme: dark) { .theme-scope { ... } } does nothing when .theme-scope is the container doing the querying, because it cannot match its own query. The practical consequence: the component that sets --theme cannot also restyle itself based on --theme. It styles its descendants, and its own appearance must come from a parent container's query or from plain CSS. In practice this shapes how I structure components. The theme-setting wrapper is a dumb shell: it declares container-type: style and sets the property, nothing else. Everything visual lives in the children, which can see the property. Once you accept the one-way flow, the architecture gets simpler, not harder.
Debugging: your query matched nothing
Walk this list. One, the ancestor has container-type: style. Setting the custom property without the container type is the most common failure, and the query silently matches nothing. Two, the value matches exactly. Style queries use equality against the computed value, so --theme: dark matches style(--theme: dark) but --theme: Dark does not. Case matters. Three, if you named the container with container-name, the query must use that name: @container sidebar style(--theme: dark). An unnamed query looks at the nearest container of any name, which may not be the one you meant. Four, check that your fallback styles are not overriding the query. Rules inside @container have normal specificity, so a later rule with equal specificity wins regardless of which block it sits in. Put the queries after the defaults. Five, confirm the property is actually set where you think it is. DevTools shows custom properties in the Computed tab, which settles inheritance arguments in seconds.
FAQ
Do I need container-type: style on the ancestor?
Yes. A style query reads from the nearest ancestor container, and an element only becomes one for style queries when it declares container-type: style. Without it, the query has nothing to read and never matches. This is the most common setup mistake.
How is this different from size container queries?
Size queries react to an element's dimensions and need container-type: inline-size. Style queries react to custom property values and need container-type: style. They are separate features with separate containment, and you can combine them: a card can restyle when it is narrow and when its section is dark.
Can I query a regular property like color or font-weight?
Not in any shipped browser. Style queries are custom-properties-only across Chrome, Safari and Firefox. Encode the state you care about as a custom property, like --theme or --density, and query that instead.
Related reading: Tailwind v4 @layer cascade fix · Tailwind button styling guide