Sub topic 11
Style Queries on Custom Properties
Size container queries ask how wide an ancestor is. Style queries ask what value a custom property has on an ancestor. This lets a component switch between visual variants, such as dark or light, or compact or comfortable, without a modifier class on every element inside it.
This chapter assumes you can already write @container rules with container-type, and that you know custom properties inherit down the tree. It also assumes you know that later rules in the cascade win when specificity is equal.
What a style query is
A style query is an @container rule whose condition is wrapped in style(). It matches when a container's computed value for a custom property equals the value you wrote.
.theme-scope {
--theme: dark;
}
@container style(--theme: dark) {
.card {
background: #222;
color: #eee;
}
}The .card styles apply only when the nearest ancestor that qualifies as a container computes --theme as dark. The .card can sit anywhere inside .theme-scope. Custom properties inherit, so every descendant of .theme-scope sees --theme: dark on its parent chain.
A style query needs no container-type. Every element is automatically a style container for custom property queries. You only declare a container when you also want size queries, or when you want to give the container a name.
The element itself is never the container
A query always looks at ancestors, never at the element being styled. This is the most common source of confusion.
<div class="theme-scope">
<article class="card">Works: the parent sets --theme</article>
</div>
<article class="card" style="--theme: dark">
Does not match: the card sets --theme on itself
</article>In the second case the card has --theme: dark, but the query looks at the card's parent, which has no value. Move the property to a wrapper, or add a wrapper element around the component when you control the markup.
Descendants deeper in the tree match too. A <h2> inside .card inside .theme-scope is queried against its parent .card. Since .card inherited --theme: dark, the value is there.
How values are compared
The browser compares computed values, not source text. What that means depends on whether the property is registered.
- Unregistered properties are compared as token streams.
darkequalsdark. Differences in whitespace around tokens are ignored, butdarkand"dark"are different tokens and do not match. - Registered properties (declared with
@property) are compared after parsing to their declared type.1remand16pxcan match in a length-typed property if they compute to the same value. - An unset unregistered property matches no value at all. It is the guaranteed-invalid value, so
style(--theme: light)fails when nobody has set--theme.
That last point is why registering a property with an initial value is a good habit for style queries:
@property --density {
syntax: "compact | comfortable";
inherits: true;
initial-value: comfortable;
}
.list-item {
padding: 1rem;
}
@container style(--density: compact) {
.list-item {
padding: 0.25rem 0.5rem;
}
}
@container style(--density: comfortable) {
.list-item {
padding: 1rem 1.5rem;
}
}Now --density always has a value. An element with no setting is comfortable, and any invalid value falls back to the initial value instead of silently matching nothing. Set the property on any region of the page, for example .sidebar { --density: compact; }, and every .list-item inside it follows.
Combining conditions
Style queries use the same and, or, and not keywords as size queries. Each style() function is its own term.
@container style(--theme: dark) and style(--density: compact) {
.list-item {
border-bottom: 1px solid #444;
}
}
@container style(--theme: dark) or style(--theme: contrast) {
.card {
color: #fff;
}
}
@container not style(--theme: dark) {
.card {
box-shadow: 0 1px 4px rgb(0 0 0 / 0.2);
}
}Be careful with not. not style(--theme: dark) is true whenever the value is anything other than dark, including when --theme is not set anywhere. If you want "explicitly light", write style(--theme: light) instead.
You can also mix both kinds of query by nesting. The outer rule checks the width, and the inner rule checks the custom property.
.card-wrapper {
container: card / inline-size;
}
@container card (min-width: 400px) {
@container style(--variant: featured) {
.card {
grid-template-columns: 12rem 1fr;
}
}
}Nesting @container inside @container is allowed. Both conditions must hold, and each is resolved against its own container.
Naming style containers
A name lets a style query target one particular ancestor instead of the nearest one. Setting only container-name is enough, because no size containment is needed for style queries.
.theme-scope {
container-name: theme;
--theme: dark;
}
.panel {
--theme: light;
}
@container theme style(--theme: dark) {
.card {
background: #222;
}
}If a .card sits inside a .panel which sits inside .theme-scope, the unnamed query would see the panel's light. The named query skips the panel, because the panel has no container-name: theme, and matches .theme-scope. Naming is how you let an inner region override a value without breaking an outer query.
A worked example: variant switching
A card should look different in a promotional slot than in a normal list. Instead of passing a class to every card and its children, you set one property on the slot.
<section class="promo-slot">
<article class="card">
<h2>Autumn sale</h2>
<p>Twenty percent off everything.</p>
</article>
</section>
<section class="list-slot">
<article class="card">
<h2>Regular item</h2>
<p>Standard presentation.</p>
</article>
</section>@property --variant {
syntax: "default | featured";
inherits: true;
initial-value: default;
}
.promo-slot {
--variant: featured;
}
.card {
padding: 1rem;
border: 1px solid #ccc;
}
@container style(--variant: featured) {
.card {
border-color: goldenrod;
background: #fffbe6;
}
.card h2 {
font-size: 1.75rem;
}
}Both cards share the same markup and the same base CSS. Only the ancestor differs. The heading rule works because the h2 is queried against its parent .card, which inherits --variant: featured from the slot.
Style queries compared with size queries
| Aspect | Size query | Style query |
|---|---|---|
| Condition | `(min-width: 400px)` | `style(--theme: dark)` |
| Requires `container-type` | Yes, `inline-size` or `size` | No |
| Side effects on the container | Containment changes layout | None |
| Value tested | Measured dimensions | Computed custom property value |
| Browser support | Broad | Narrower |
Because a style query adds no containment, you can use it on any element without the sizing side effects covered earlier in this tutorial.
Support and fallbacks
Support for style queries on custom properties arrived later than size queries and is not in every browser. Check a current compatibility table before depending on it. Querying regular properties such as style(color: red) has even less support, so stay with custom properties.
The @supports (container-type: inline-size) test from the earlier fallback chapter does not tell you whether style queries work. A safer approach is to write the default look outside any query. A browser that cannot parse style() drops the whole @container block and shows the default.
.card {
background: #fff;
color: #111;
}
@container style(--theme: dark) {
.card {
background: #222;
color: #eee;
}
}Design so the default is acceptable on its own. If dark mode is essential to the product, also set a class or use prefers-color-scheme as the primary mechanism, and treat the style query as an enhancement.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Rule never applies | The property is set on the styled element itself | Set it on an ancestor or wrapper |
| Rule never applies | Property is unset and unregistered | Register it with `@property` and an `initial-value` |
| Rule matches too often | `not style(...)` also matches unset values | Query the opposite value explicitly |
| Value looks right but fails | Quoting differs, such as `"dark"` versus `dark` | Use the same token form in both places |
| Wrong region wins | A closer ancestor sets a different value | Name the container and query by name |
| Nothing works in one browser | Style queries unsupported | Provide a default outside the query |
When debugging, open the browser's developer tools, select the parent of the element you are styling, and read the computed value of the custom property there. If that value is not what your style() expects, the query cannot match.
Exercise
A .badge element has style="--tone: warning" set directly on it. Your stylesheet contains @container style(--tone: warning) { .badge { background: orange; } }. The badge stays unstyled. Why, and what are two ways to fix it?
Show answer
A style query is evaluated against an ancestor of the element, never the element itself, so the badge's own --tone is ignored. Fix it by moving --tone: warning to a parent or wrapper element, or by wrapping the badge in a new element that carries the property.
Exercise
You write @container not style(--theme: dark) { .card { box-shadow: 0 1px 4px gray; } }. A page never sets --theme anywhere. Does the shadow apply, and how would you limit the rule to pages that explicitly set --theme: light?
Show answer
The shadow applies, because an unset property is not equal to dark, so the not condition is true. To limit it, query the value directly with @container style(--theme: light). You can also register --theme with @property and an initial-value, so the property always has a defined value.