Beyond size: what container style queries will query
Container queries are still at an early stage. Chromium already supports them, Safari began supporting them in version 16, and Firefox is presumably not far behind. Early discussion of the feature tends to compare its syntax with media queries.
/* Stacked flex container */
.post {
display: flex;
flex-direction: column;
}
/* Change direction when viewport is 600px or wider */
@media(min-width: 600px) {
.post {
flex-direction: row;
}
}
/* Define the container */
.posts {
container-name: posts;
container-type: inline-size;
}
.post {
display: flex;
flex-direction: column;
}
/* Query the container's min-width */
@container posts (min-width: 600px) {
/* Change styles when `posts` container is 600px or wider */
.post {
flex-direction: row;
}
}
Both examples query min-width: 600. The media query reacts to the viewport width; the container query reacts to the computed width of the .posts element.
CSS Podcast Episode 59, with Una and Adam, pushed the conversation toward what comes next: style queries. The current working draft of the CSS Containment Module Level 3 spec defines container style queries:
A container style query allows querying the computed values of the query container. It is a boolean combination of individual style features (<style-feature>) that each query a single, specific property of the query container.
The draft gives no syntax examples, only a description:
The syntax of a <style-feature> is the same as for a declaration, and its query is true if the computed value of the given property on the query container matches the given value (which is also computed with respect to the query container), unknown if the property or its value is invalid or unsupported, and false otherwise. The boolean syntax and logic combining style features into a style query is the same as for CSS feature queries. (See @supports.)
In time, that should let us write something like the following:
.posts {
container-name: posts;
}
@container posts (background-color: #f8a100) {
/* Change styles when `posts` container has an orange background */
.post {
color: #fff;
}
}
The example is deliberately trivial. Note that container-type here is no longer based on the container's inline-size but on style, which could be declared like so:
.posts {
container-name: posts;
container-type: style; /* unnecessary */
}
As things stand today, all container queries are style queries by default. Miriam Suzanne has outlined the possible issues that default could raise.
Possible use cases
Practical applications aren't clear yet, but a few directions come to mind:
- Custom property values: custom properties have already been used as state indicators, as in the DRY-switching method Ana covered. The value changes, and so do the styles.
- An alternate dark mode approach: rather than reassigning custom property values through a body class change, an entire color palette could change when, for instance, the body background color changes.
- More complex query conditions: applying styles when the
sizeandstyleconditions for a container are both met.
On the CSS Podcast, Una also suggested container style queries could head off awkward styling cases, such as italicized text inside an already italicized blockquote:
blockquote {
container-name: quote;
}
@container quote (font-style: italic) {
em, i, q, address {
font-style: normal;
}
}


