We’re still in suuuuuper early days with container queries. Too early for broad browser support, but Chromium already supports it, Safari started supporting it in version 16, and Firefox is presumably not far behind.
Most early days conversations revolving around container queries usually compare the syntax to 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 of these are making queries for min-width: 600
. The difference is that the media query is looking at the viewport’s width to trigger those style changes while the container query is looking at the computed width of the .posts
element. Sweet!
But after listening to CSS Podcast Episode 59, Una and Adam poked at the future of container queries: 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.
But no examples on syntax just yet — only a brief 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.)
So, yes, given time we should expect to pull off something like this:
.posts { container-name: posts;
} @container posts (background-color: #f8a100) { /* Change styles when `posts` container has an orange background */ .post { color: #fff; }
}
That’s a pretty dumb example. One thing to note is that the container-type
is no longer based on the container’s inline-size
but by style
. We could delcare that like so:
.posts { container-name: posts; container-type: style; /* unnecessary */
}
…but all container queries are style
queries by default. Well. at least as it stands today. Miriam Suzanne has a nice outline of the possible issues that might pop up with that.
Where might querying a container’s styles come in handy? I don’t know yet! But my mind gos to a few places:
- Custom property values: We’ve seen custom properties used like state indicators, such as the DRY-switching method Ana covered a while back. The value changes, and so do styles.
- Alternate dark mode approach: Instead of basing it all on a body class change that re-assigns custom property values, maybe we can change an entire color palette if, say, the body background changes color.
- More complex query conditions: Like, say, we want to apply styles when the
size
andstyle
conditions for a container are met.
Una also mentioned in the CSS Podcast that container style queries could help prevent some awkward styling situations, like if we happen to have italicized text in an already italicized blockquote
:
blockquote { container-name: quote;
} @container quote (font-style: italic) { em, i, q, address { font-style: normal; }
}