I wrote up some early thoughts on container style queries a little while back. It’s still early days. They’re already defined in the CSS Containment Module Level 3 specification (currently in Editor’s Draft status) but there are still a couple of outstanding discussions taking place.
The basic idea is that we can define a container and then apply styles conditionally to its descendants based on its computed styling.
@container <name>? <conditions> { /* conditional styles */ }
The best example I’ve seen so far is removing italics from something like , , and That’s the general idea. But if you didn’t know it, Miriam Suzanne, who is an editor of the spec, keeps an ongoing and thorough set of personal notes on container style queries that is publicly available. It was updated the other day and I spent some time in there trying to wrap my head around more nuanced aspects of style queries. It’s unofficial stuff, but I thought I’d jot down some things that stood out to me. Who knows? Maybe it’s stuff we can eventually look forward to! We don’t even need to explictly assign a container-name or container-type to define a style container because everything is a style container by default. So, you see that example above that removes italics? Notice it doesn’t identify a container. It jumps right to the query using the style() function. So, what container is being queried? It’s going to be the direct parent of the elements receiving the applied styles. And if not that, then it’s the next nearest relative container that takes precedence. I like that. It’s very CSS-y for the query to search up for a match, then continue to bubble up until it finds a matching condition. It was hard for my little brain to understand why we can get away with an implicit container based on styles but not so much when we’re dealing with dimensional queries, like size and inline-size. Miriam explains it nicely: Dimensional queries require css containment on the size, layout, and style of the container in order to prevent layout loops. Containment is an invasive thing to apply broadly, so it was important that authors have careful control over what elements are (or are not) size containers. Style-based queries don’t have the same limitation. There is already no way in CSS for descendant styles to have an impact on the computed styles of an ancestor. So no containment is required, and there are no invasive or unexpected side-effects in establishing an element as a style query container. It all comes down to consequences — of which there are none as far as everything being a style query container right out of the box. That’s the same “forgiving” spirit as the rest of CSS. Let’s say we want define a style query without an explicit container-name: This works because all elements are style containers, no matter the container-type. That’s what allows us to implicitly query styles and rely on the nearest match. And this is totally fine since, again, there are no adverse side effects when establishing style containers. We have to use an explicit container-type for dimensional queries, but not so much for style queries since every element is a style query. That also means this container is both a style and dimensional query: Perhaps we don’t want a container to participate in the matching process. That’s where it might be possible to set container-type: none on an element. If, say, we were to write a style query for padding , there is no reliable way to determine the best matching container, regardless of whether we’re working with an explicitly named container or the nearest direct parent. That’s because padding is not an inherited property. So, in those instances, we ought to use container-name to explictly inform the browser which containers they can pull from. We can even give a container multiple explicit names to make it match more conditions: Oh, and container-name accepts any number of optional and reusable names for a container! That’s even more flexibility when it comes to helping the browser make a choice when searching for matches. I sort of wonder if that might also be considered a “fallback” in the event that one container is passed over. The or and and operators allow us to combine wueries to keep things DRY: There’s a little overlap between container style queries and work being done to define a toggle() function. For example, we can cycle through two font-style values, say italic and normal: Cool. But the proposal for CSS Toggles suggests that the toggle() function would be a simpler approach: But anything beyond this sort of binary use case is where toggle() is less suitable. Style queries, though, are good to go. Miriam identifies three instances where style queries are more suitable than a toggle(): Notice that style queries are a formal solution for the “CSS custom property toggle trick”. In there, we set an empty custom property (--foo: ;) and use the comma-separated fallback method to “toggle” properties on and off when then custom property is set to a real value. That’s super cool, also a lot of work that style container queries makes trivial. For generated content produced by the content property of ::before and ::after pseudo-elements, the matching container is the element on which the content is generated. We can define a web component as a container and query it by style. First, we have the of the component:
Then we use the :host pseudo-element as a container to set a container-name, a container-type, and some high-level attributes on it: Elements inside the Again, all the stuff I’ve jotted down here is based on Miriam’s notes, and those notes are not a substitute for the official spec. But they are an indication of what’s being discussed and where things could land in the future. I appreciate Miriam linked up a handful of outstanding discussions still taking place that we can follow to stay on top of things: The above is the detailed content of Digging Deeper Into Container Style Queries. For more information, please follow other related articles on the PHP Chinese website! when they are used in a context where content is already italicized:
em, i, q {
font-style: italic; /* default UA behavior */
}
/* When the container's font-style is italic, remove italics from these elements. */
@container style(font-style: italic) {
em, i, q {
font-style: normal;
}
}
Every element is a style container
A container can support both dimensional and style queries
@container <name>? <conditions> {
/* conditional styles */
}
em, i, q {
font-style: italic; /* default UA behavior */
}
/* When the container's font-style is italic, remove italics from these elements. */
@container style(font-style: italic) {
em, i, q {
font-style: normal;
}
}
Excluding a container from being queried
@container style(font-style: italic) {
em {
font-style: normal;
}
}
Explicit style query containers offer more control of what gets queried
.card-container {
container: card / inline-size; /* implictly a style query container as well */
}
.some-element {
container-type: none;
}
Style queries can be combined
@container <name>? <conditions> {
/* conditional styles */
}
Toggling styles
em, i, q {
font-style: italic; /* default UA behavior */
}
/* When the container's font-style is italic, remove italics from these elements. */
@container style(font-style: italic) {
em, i, q {
font-style: normal;
}
}
@container style(font-style: italic) {
em {
font-style: normal;
}
}
.card-container {
container: card / inline-size; /* implictly a style query container as well */
}
Style queries solve the “Custom Property Toggle Hack”
.some-element {
container-type: none;
}
Style queries and CSS generated content
.card {
container-name: card layout theme;
}
Style queries and web components
.theme {
container-name: theme;
}
.grid {
container-name: layout;
}
.card {
container-name: card layout theme;
}
@container bubble style(--arrow-position: start start) or style(--arrow-position: end start) {
.bubble::after {
border-block-end-color: inherit;
inset-block-end: 100%;
}
}
/* is the same as... */
@container bubble style(--arrow-position: start start) {
/* etc. */
}
@container bubble style(--arrow-position: end start) {
/* etc. */
}
em, i, q {
font-style: italic;
}
@container style(font-style: italic) {
em, i, q {
font-style: normal;
}
}
What’s next?