Sub topic 9
Style queries with @container style()
Size queries ask how big a container is. A style query asks what a container's custom properties are set to. This chapter covers how @container style() works and how to build component variants and themes with it. It also covers the mistakes that make a style query silently fail.
This chapter assumes you already know:
- how
@containersize queries work, includingcontainer-typeandcontainer-name - CSS custom properties (
--nameandvar()) - how inheritance and the cascade work
What a style query asks
A style query is an @container rule whose condition is style(...). The condition holds a custom property and a value:
@container style(--tone: warning) {
.alert {
border-color: orange;
}
}The browser finds the query container for .alert, reads that container's computed value of --tone, and compares it with warning. If they match, the rules inside apply to .alert.
The rules inside a container query never style the container itself. They style descendants only. That rule is the same for size queries and style queries.
A complete example
The markup has a region that sets the tone and an alert inside it:
<div class="alert-region">
<p class="alert">Your session expires in two minutes.</p>
</div>.alert-region {
--tone: warning;
}
.alert {
border: 2px solid gray;
padding: 0.75rem;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
background: #fff4e0;
}
}Without --tone: warning on the region, the alert keeps its gray border. The region's only job is to set a value, and the component reads it. The alert's CSS contains no selector for .alert-region, so the alert works in any context that sets --tone.
How the browser matches a style query
Style queries differ from size queries in three ways.
First, no container-type is needed. Every element is a style query container by default, because a style query only reads computed values and needs no layout containment. That means no side effects on layout. Size containment and its costs only come with container-type: size or inline-size.
Second, the query reads the computed value of the container. For a custom property, that value includes inheritance. Custom properties inherit by default, so a --tone set on .alert-region is also the computed --tone of every element inside it. The nearest ancestor of .alert is its parent. The parent has inherited --tone: warning from the region, so the query matches even when the region is several levels up.
Third, the match compares the value you wrote with the computed value. An element that does not set the property and inherits nothing has no value, so style(--tone: warning) does not match.
Targeting a named container
You can name the container you want, as with size queries. A name also prevents accidental matches from a nearer ancestor:
<section class="theme-scope">
<div class="panel">
<p class="note">Notes follow the theme scope.</p>
</div>
</section>.theme-scope {
container-name: theme;
--theme: dark;
}
.panel {
--theme: light;
}
.note {
color: black;
}
@container theme style(--theme: dark) {
.note {
color: white;
}
}The .panel sets --theme: light, and it is the nearest ancestor of .note. An unnamed style(--theme: dark) query would read .panel and fail. The named query skips .panel and reads .theme-scope, so .note turns white.
Notice that container-name alone is enough here. Because style queries need no container-type, you can name an element without turning on any size containment.
Combining conditions
Style conditions combine with and, or and not:
@container style(--tone: warning) or style(--tone: danger) {
.alert {
font-weight: 600;
}
}
@container not style(--tone: info) {
.alert {
border-width: 3px;
}
}You can also mix a size condition and a style condition in one query. The container must then be a size container too, so give it a type and a name:
<div class="alert-region">
<p class="alert">Disk space is almost full.</p>
</div>.alert-region {
container: alerts / inline-size;
--tone: warning;
}
.alert {
padding: 0.5rem;
}
@container alerts (min-width: 30rem) and style(--tone: warning) {
.alert {
padding: 1rem 1.5rem;
border-left: 0.5rem solid orange;
}
}The container shorthand sets the name and type together. Here the alert gets the wide warning layout only when the region is at least 30rem wide and its tone is warning.
Variants and themes through custom property queries
The main use of style queries is to separate a component's variants from the markup that places it. A parent, a utility class or a data attribute sets a property, and the component responds.
<article class="card" data-variant="feature">
<h2 class="card-title">Release notes</h2>
<p>What changed this month.</p>
</article>.card[data-variant="feature"] {
--variant: feature;
}
.card-title {
font-size: 1.125rem;
}
@container style(--variant: feature) {
.card-title {
font-size: 1.75rem;
letter-spacing: 0.02em;
}
}The .card carries the property and .card-title, its descendant, reads it. This works because .card is the title's parent. Without a style query you would write .card[data-variant="feature"] .card-title. That selector ties the title's styles to the card's markup and adds specificity. The style query keeps the title's rules in one block, keyed on a value.
A design token pattern uses the same idea for themes. A single --theme property on the page root or on a section lets every component adapt with no theme class on each component:
:root {
--theme: light;
}
.promo {
--theme: dark;
}
.button {
background: white;
color: #222;
}
@container style(--theme: dark) {
.button {
background: #222;
color: white;
}
}Any .button inside .promo is dark. Any .button elsewhere is light. Nesting works as you would expect, because the nearest ancestor's computed value wins.
Using the custom property in two ways
A custom property can drive both a style query and a normal var() lookup. Use a query for discrete choices (which variant) and var() for continuous values (how large):
.card {
--accent: steelblue;
border-top: 4px solid var(--accent);
}
.card[data-variant="feature"] {
--variant: feature;
--accent: orange;
}The --variant property is a switch for a query and --accent is a value read through var().
Registered properties and what counts as equal
A plain custom property is just a token sequence, so style(--tone: warning) compares text. A registered property, declared with @property, has a type, and the comparison uses the typed computed value:
@property --density {
syntax: "<number>";
inherits: true;
initial-value: 1;
}
.compact {
--density: 2;
}
.row {
padding: 1rem;
}
@container style(--density: 2) {
.row {
padding: 0.25rem;
}
}Registering a property gives you three things:
- a default (
initial-value), so the property always has a value to compare - a type, so
2and2.0compare as the same number - control over inheritance through
inherits
Be careful with inherits: false. A non-inherited property exists only on the element that sets it. A style query reads the nearest ancestor, so it only sees the value when the setting element is the direct parent.
Pitfalls
Styling the container itself
A query never matches the element that carries the property. If you write this, the .alert never changes:
.alert {
--tone: warning;
border: 2px solid gray;
}
@container style(--tone: warning) {
.alert {
border-color: orange;
}
}The .alert sets --tone, but its container is its parent, which has no --tone. Either move the property to a wrapper or style the element directly with a normal selector. A child of .alert would match, because the .alert is then its parent.
A nearer container with a different value
Unnamed queries use the nearest ancestor. If a middle element sets a different value, as .panel did above, the outer value is hidden. Use a container name when you need a specific ancestor.
Values that look equal but are not
For unregistered properties, warning and "warning" are different tokens. Write the value in the query exactly as you set it, or register the property so it has a type.
Expecting a fallback value
A missing property does not match any style(--x: value) query. Put your default styles outside the query, as in all the examples here, and let the query override them. A not style(...) condition is true when the property is absent, so use it with care.
Specificity does not change
Rules inside @container have the same specificity as the same rules outside it. A later, equally specific rule outside the query still overrides the query's rule. Keep the base rule before the query.
Browser support and fallback
Size queries and style queries have different support. Size queries are available in all current major browsers. Style queries on custom properties arrived later and are not available everywhere, so check a current compatibility table for the browsers you target. Because an unsupported @container style(...) block is ignored, the base styles act as the fallback. Write the base styles so the component looks acceptable without the query, then let the query enhance it.
The @supports approach from the size query chapters does not help here. @supports (container-type: inline-size) tests size queries, not style queries. If a feature must work without style queries, set the variant through a regular selector, for example .card[data-variant="feature"] .card-title, and add the style query only for the cases that can degrade.
Summary table
| Question | Answer |
|---|---|
| Does the container need `container-type`? | No. Every element can be a style query container. |
| What does the query read? | The container's computed custom property value. |
| Which element is the container? | The nearest ancestor, or the nearest with the given name. |
| Can it match the element that sets the property? | No. Only descendants are styled. |
| What if the property is not set? | `style(--x: value)` does not match. |
Exercise
A .badge inside a .toolbar should turn red when an ancestor sets --status: error. The author wrote .badge { --status: error; } and @container style(--status: error) { .badge { background: red; } }. The badge stays unchanged. Explain why and fix it.
Show answer
The query targets the .badge's nearest ancestor container, but the .badge itself sets --status. A query never styles the element that holds the value, and the parent has no --status. Move the declaration to an ancestor, for example .toolbar { --status: error; }, and keep the @container style(--status: error) block. The .badge's parent now inherits error, so the query matches.
Exercise
A .theme-scope sets --theme: dark. Inside it, a .panel sets --theme: light, and a .note sits inside .panel. You want .note to follow .theme-scope. What change to the query achieves this?
Show answer
Give .theme-scope a name with container-name: theme, and name the container in the query: @container theme style(--theme: dark) { ... }. The named query skips .panel and reads .theme-scope, so .note matches the dark rule.