Sub topic 10
Variants and themes through custom property queries
This chapter assumes you know custom properties (--name and var()), basic @container rules, and how a query picks its nearest ancestor container. It builds on style queries and applies them to variants and themes.
Why query a custom property
A size query asks how wide a container is. A style query asks what value a custom property has on an ancestor. That lets a parent declare intent, such as "this region is a warning", and lets any descendant react without a modifier class on every child.
Custom properties inherit, so a value set on a distant ancestor is the computed value on every element below it. The style query compares against the computed value of the nearest ancestor container, which includes inherited values.
A variant example
The wrapper sets --tone. The component supplies a default and one rule per variant.
<div class="alert-region">
<p class="alert">Disk space is low.</p>
</div>
<div class="alert-region" style="--tone: danger">
<p class="alert">Backup failed.</p>
</div>.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
padding: 0.5rem 1rem;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}
@container style(--tone: danger) {
.alert {
border-color: crimson;
background: #fde8ea;
}
}The inline style on the second region overrides --tone for that region only, so the two alerts get different colors from the same component CSS. The .alert rule is the default. The variant rules only add to it.
Combining conditions
Style queries accept and, or, and not. Wrap each condition in style():
@container style(--tone: warning) or style(--tone: danger) {
.alert {
font-weight: 600;
}
}
@container not style(--tone: danger) {
.alert {
border-style: dashed;
}
}Be careful with not. It also matches when --tone is not set at all, because "not equal to danger" is true for an empty value. Pair it with an explicit default if that matters.
Themes
A theme works the same way, but the property usually sits near the page root and the values are keywords you control.
:root {
--theme: light;
}
.sidebar {
--theme: dark;
}
.button {
background: white;
color: #222;
}
@container style(--theme: dark) {
.button {
background: #222;
color: white;
}
}A .button inside .sidebar is dark. A .button anywhere else is light. Nesting works as you would expect: if a light panel sits inside the dark sidebar and sets --theme: light, buttons inside the panel see light, because the nearest value wins.
Naming the container
An unnamed style query uses the nearest ancestor. If an unrelated container sits between the component and the element that sets the property, the value still reaches the component through inheritance, so this usually works. A name is useful when you want to be explicit, for example @container region style(--tone: warning). Naming requires that the element has container-name: region.
.alert-region {
container-name: region;
--tone: warning;
}
@container region style(--tone: warning) {
.alert {
border-color: orange;
}
}Pitfalls
- The element itself is never styled. A rule inside
@container style(--tone: warning)cannot style the element that sets--tone. Only descendants are affected. Put the property on a wrapper and the styled element inside it. - Values compare as written.
warningmatcheswarning. Do not rely on matching a computed length or color in a different notation, such as#f00againstred. Use keywords for variants. - Typos fail silently.
--tone: warninmatches nothing, and the default styling applies. Keep the allowed values in one place, such as a comment or a design token file. - Support differs by browser. Style queries for custom properties arrived later than size queries and may be missing in some browsers. Check current support tables before shipping. Write the default as the complete, usable design so unsupported browsers still see something sensible.
Exercise
A .badge sits inside .panel, and .panel itself has --tone: danger set on it. A rule @container style(--tone: danger) { .panel { color: red; } } does not turn the panel red. Why, and how do you fix it?
Show answer
A container query only styles descendants of the container, never the container itself. Here .panel is the element that sets --tone, so the rule cannot target it. Move --tone: danger to a wrapper around .panel, or style .badge instead.