Container queries can also evaluate the computed style of the container element. A container style query is a @container
query that uses one or more style()
functional notations. The boolean syntax and logic combining style features into a style query is the same as for CSS feature queries.
@container style(<style-feature>),
not style(<style-feature>),
style(<style-feature>) and style(<style-feature>),
style(<style-feature>) or style(<style-feature>) {
/* <stylesheet> */
}
The parameter of each style()
is a single <style-feature>
. A <style-feature>
is a valid CSS declaration, a CSS property, or a <custom-property-name>
.
@container style(--themeBackground),
not style(background-color: red),
style(color: green) and style(background-color: transparent),
style(--themeColor: blue) or style(--themeColor: purple) {
/* <stylesheet> */
}
A style feature without a value evaluates to true if the computed value is different from the initial value for the given property.
If the <style-feature>
passed as the style()
function's argument is a declaration, the style query evaluates to true if the declaration's value is the same as the computed value of that property for the container being queried. Otherwise, it resolves to false.
The following container query checks if the computed_value
of the container element's --accent-color
is blue
:
@container style(--accent-color: blue) {
/* <stylesheet> */
}
Note: If a custom property has a value of blue
, the equivalent hexadecimal code #0000ff
will not match unless the property has been defined as a color with @property
so the browser can properly compare computed values.
Style features that query a shorthand property are true if the computed values match for each of its longhand properties, and false otherwise. For example, @container style(border: 2px solid red)
will resolve to true if all 12 longhand properties (border-bottom-style
, etc.) that make up that shorthand are true.
The global revert
and revert-layer
are invalid as values in a <style-feature>
and cause the container style query to be false.