Sub topic 11
Style Queries on Custom Properties
Size queries ask how big a container is. Style queries ask what a container's custom properties are set to. This chapter covers the style() function inside @container, how it resolves, and the errors people hit most.
This chapter assumes you know CSS custom properties, the cascade and inheritance, and the basic @container rule from earlier chapters.
The idea
A style query matches on the computed value of a custom property on an ancestor. You can use it to switch a component's look based on a "mode" that a parent sets, without adding a class to every child.
.theme-scope {
--theme: dark;
}
@container style(--theme: dark) {
.card {
background: #222;
color: #eee;
}
}The .card rule applies when .card has an ancestor container whose computed --theme is dark. The ancestor here is .theme-scope.
How it resolves
A style query looks at the nearest ancestor container, as size queries do. Custom properties inherit, so the nearest ancestor sees the value whether it set it or received it from higher up.
An element can be queried only through its ancestors. A style query never matches on the element's own custom properties. If .card sets --theme: dark itself, the rule above does not use that value.
flowchart TDReplace nothing here. The diagram below is the one that belongs in this chapter.
Do you need container-type?
For style queries on custom properties, no. Every element is a style container by default, so you don't need container-type on .theme-scope. You only need container-type when you also run size queries on the same element.
Naming a style container
You can name a container and target it, as with size queries.
.panel {
container-name: panel;
--density: compact;
}
@container panel style(--density: compact) {
.item {
padding: 0.25rem;
}
}The name restricts the match to the ancestor called panel. A closer ancestor with a different name is skipped.
Combining with size queries
You can combine both conditions in one rule. The container must satisfy both, so it needs container-type: inline-size for the width part.
.card-wrapper {
container-type: inline-size;
--layout: featured;
}
@container style(--layout: featured) and (min-width: 500px) {
.card {
grid-template-columns: 12rem 1fr;
}
}Common errors
- The value doesn't match. The query compares computed values. Whitespace and case in keywords can matter, so keep the declared value and the query value consistent.
- The property is set on the element itself. Move it to an ancestor, or to a wrapper element.
- The property is not a custom property. Support for style queries on regular properties like
coloris limited. Stick to custom properties in production. - Browser support is uneven. Custom property style queries were supported first in Chromium and Safari. Firefox support came later. Check current support tables and gate the styles.
- A closer container hides the value. A nearer named or unnamed container is found first. Use
container-nameto target the right one.
.card {
background: white;
}
@supports (container-type: inline-size) {
@container style(--theme: dark) {
.card {
background: #222;
}
}
}The @supports check tests for container query support in general, not style queries specifically. The .card default above still works when the query is ignored, so design a light default and treat the dark style as an enhancement.
When to use them
Use style queries when a parent sets a design decision such as theme, density, or layout variant, and many descendants respond. For one or two elements, a class is simpler.
Exercise
A .card sets --theme: dark on itself, and no ancestor sets --theme. A rule @container style(--theme: dark) { .card { color: #eee; } } exists. Does the rule apply? How would you fix it?
Show answer
It does not apply. A style query reads the computed custom property of an ancestor container, never the element's own value. Move --theme: dark to a wrapper or ancestor element, such as .theme-scope, that contains the card.