Sub topic 9
Style queries with @container style()
Size queries ask how big a container is. Style queries ask what a container's computed styles are. This chapter covers style queries on custom properties, which is the form with the widest browser support. It assumes you know CSS custom properties (--name and var()), the cascade and inheritance, and the basics of @container size queries.
What a style query does
A style query is an @container rule whose condition is style(...). The browser finds a query container, reads that container's computed value for the property in the condition, and applies the rules inside only if it matches.
.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}<div class="alert-region">
<p class="alert">Disk space is low.</p>
</div>The .alert has no --tone rule of its own. It gets the warning look because an ancestor's computed --tone is warning.
No container-type needed
Size queries need container-type: inline-size (or size). Style queries on custom properties do not. Every element is automatically a style query container, so .alert-region needs no extra declaration.
Custom properties inherit by default. The parent of .alert therefore has a computed --tone of warning even when the declaration sits several levels higher.
How the container is chosen
For a style query with no name, the query container is the nearest ancestor, which is the parent element. Its computed value includes inherited values, so a declaration on a distant ancestor still matches. The closest declaration wins, because it overrides the inherited one.
.alert-region { --tone: warning; }
.sidebar { --tone: info; }An .alert inside .sidebar, which is inside .alert-region, sees info. The inner declaration overrides the outer one.
If you give the query a container name, for example @container card style(--tone: warning), the browser skips ancestors until it finds one named card. That element needs container-name: card. Its computed --tone is then the value that is checked.
Rules to remember
- The match compares the computed value of the custom property, as a token sequence. Write the value in the query the same way you write it in the declaration.
- A rule inside
@containerstyles descendants of the container only. It cannot style the container itself. Putting--tone: warningand the matching.alertstyling on the same element does nothing. - Without a value,
style(--tone)tests whether the property is set to something other than its initial value. Support for this form varies, so prefer explicit values. - You can combine conditions with
and,orandnot, for examplestyle(--tone: warning) and style(--size: large).
Variants without extra classes
Style queries let a component expose a small set of variants through one custom property. The page sets the property, and the component decides how to look.
.badge {
padding: 0.25rem 0.75rem;
border-radius: 999px;
background: #e5e7eb;
}
@container style(--tone: success) {
.badge { background: #dcfce7; color: #166534; }
}
@container style(--tone: danger) {
.badge { background: #fee2e2; color: #991b1b; }
}Any ancestor can now set --tone: success or --tone: danger on a section, a card or the body. The same property also works for themes, which the next chapter covers in more detail.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Setting `--tone` and styling on the same element | Nothing matches | Move the styled element inside the container |
| Querying a value that differs from the declaration, such as `Warning` vs `warning` | No match | Use the exact same token |
| Using a named query without `container-name` on an ancestor | No container is found, so the rule never applies | Add `container-name` to the intended ancestor |
| Expecting `style(color: red)` to work everywhere | Support for non-custom properties is limited | Query custom properties |
Exercise
A page has <section class="panel"><div class="inner"><p class="note">Hi</p></div></section>. The CSS sets .panel { --tone: danger; } and contains @container style(--tone: danger) { .note { color: red; } }. Does the note turn red? What happens if .inner sets --tone: info?
Show answer
Yes. The query container for .note is its parent .inner, and its computed --tone is inherited from .panel as danger, so the rule applies. If .inner sets --tone: info, its computed value is info, the query no longer matches, and the note keeps its default colour.