{"id":375607,"date":"2022-12-01T05:59:06","date_gmt":"2022-12-01T13:59:06","guid":{"rendered":"https:\/\/css-tricks.com\/?p=375607"},"modified":"2022-12-01T14:25:32","modified_gmt":"2022-12-01T22:25:32","slug":"digging-deeper-into-container-style-queries","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/digging-deeper-into-container-style-queries\/","title":{"rendered":"Digging Deeper Into Container Style Queries"},"content":{"rendered":"\n
I wrote up some early thoughts on container style queries<\/a> a little while back. It’s still early days. They’re already defined in the CSS Containment Module Level 1 specification<\/a> (currently in Editor’s Draft status) but there’s still a couple of outstanding discussions taking place.<\/p>\n\n\n\n The basic idea is that we can define a container and then apply styles conditionally to its descendants based on its computed styling.<\/p>\n\n\n\n The best example I’ve seen so far is removing italics from something like 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<\/a> 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!<\/p>\n\n\n\n\n\n\n We don’t even need to explictly assign a So, you see that example above that removes italics? Notice it doesn’t identify a container. It jumps right to the query using the 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.<\/p>\n\n\n\n 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 Dimensional queries require css containment<\/em> 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.<\/p> Style-based queries don\u2019t 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.<\/strong> So no containment is required, and there are no invasive or unexpected side-effects in establishing an element as a style query container<\/em>.<\/p>(Emphasis mine)<\/cite><\/blockquote>\n\n\n\n It all comes down to consequences \u2014 of which there are none as far as everything being a style query container right out of the box. <\/p>\n\n\n\n That’s the same “forgiving” spirit as the rest of CSS<\/a>.<\/p>\n\n\n Let’s say we want define a style query without an explicit This works because all elements are style containers<\/em>, no matter the We have to use an explicit Perhaps we don’t want a container to participate in the matching process. That’s where it might be possible to set If, say, we were to write a style query for So, in those instances, we ought to use Oh, and I sort of wonder if that might also be considered a “fallback” in the event that one container is passed over.<\/p>\n\n\n The There’s a little overlap between container style queries and work being done to define a Cool. But the proposal for CSS Toggles suggests that the But anything beyond this sort of binary use case is where Notice that style queries are a formal solution for the “CSS custom property toggle trick”<\/a>. In there, we set an empty custom property ( That’s super cool, also a lot of work that style container queries makes trivial.<\/p>\n\n\n For generated content produced by the We can define a web component as a container and query it by style. First, we have the Then we use the 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:<\/p>\n\n\n\n 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 1 specification (currently in Editor’s Draft status) but there’s still a couple of outstanding discussions taking place. The basic idea is that we can define a container and […]<\/p>\n","protected":false},"author":2508,"featured_media":374336,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"Digging Deeper Into Container Style Queries by @geoffreygraham","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[510],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/10\/container-style-queries.png?fit=1200%2C600&ssl=1","jetpack-related-posts":[{"id":374330,"url":"https:\/\/css-tricks.com\/early-days-of-container-style-queries\/","url_meta":{"origin":375607,"position":0},"title":"Early Days of Container Style Queries","date":"October 12, 2022","format":false,"excerpt":"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. \/*\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/10\/container-style-queries.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":339138,"url":"https:\/\/css-tricks.com\/next-gen-css-container\/","url_meta":{"origin":375607,"position":1},"title":"Next Gen CSS: @container","date":"May 11, 2021","format":false,"excerpt":"Chrome is experimenting with @container, a property within the CSS Working Group Containment Level 3 spec being championed by Miriam Suzanne of Oddbird, and a group of engineers across the web platform. @container brings us the ability to style elements based on the size of their parent container. The @container\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/05\/Screen-Shot-2021-05-11-at-7.38.01-AM.png?fit=1200%2C550&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":300366,"url":"https:\/\/css-tricks.com\/the-origin-story-of-container-queries\/","url_meta":{"origin":375607,"position":2},"title":"The Origin Story of Container Queries","date":"December 16, 2019","format":false,"excerpt":"Container queries don\u2019t exist today but a lot of web developers have been arguing in their favor lately. At first, the idea sounds relatively simple: whereas media queries allow us to make style changes based on the width of the browser, container queries would allow us to make style updates\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/container-queries.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":360320,"url":"https:\/\/css-tricks.com\/a-new-container-query-polyfill-that-just-works\/","url_meta":{"origin":375607,"position":3},"title":"A New Container Query Polyfill That Just Works","date":"January 6, 2022","format":false,"excerpt":"There is an easy-to-use CSS container query polyfill now. You essentially conditionally load it and forget about it. Then write spec-compliant container queries code.","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-01-at-7.02.53-AM.png?fit=759%2C480&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":342519,"url":"https:\/\/css-tricks.com\/media_queries_in_times_of_container\/","url_meta":{"origin":375607,"position":4},"title":"Media Queries in Times of @container","date":"June 15, 2021","format":false,"excerpt":"Max B\u00f6ck took me up on my challenge to look through a codebase and see how many of the @media queries could ultimately become @container queries. I took the bait and had a look at some of my projects - and yes, most of what I use\u00a0@media\u00a0for today can probably\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/container-queries.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":373087,"url":"https:\/\/css-tricks.com\/ishadeeds-container-queries-lab\/","url_meta":{"origin":375607,"position":5},"title":"iShadeed’s Container Queries Lab","date":"September 1, 2022","format":false,"excerpt":"Ahmad Shadeed got an early jump on container queries and has a growing collection of examples based on everyday patterns. And, if you missed it, his latest post on container queries does a wonderful job covering how they work since landing in Chrome 105 this month (we'll see them in\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/08\/ishadeed-container-query-example-thumbs.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/10\/container-style-queries.png?fit=1024%2C512&ssl=1","_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/375607"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/2508"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=375607"}],"version-history":[{"count":9,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/375607\/revisions"}],"predecessor-version":[{"id":375746,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/375607\/revisions\/375746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media\/374336"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=375607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=375607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=375607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}@container <name>? <conditions> {\n \/* conditional styles *\/\n}<\/code><\/pre>\n\n\n\n
<em><\/code>,
<i><\/code>, and
<q><\/code> when they are used in a context where content is already italicized:<\/p>\n\n\n\n
em, i, q {\n font-style: italic; \/* default UA behavior *\/\n}\n\n\/* When the container's font-style is italic, remove italics from these elements. *\/\n@container style(font-style: italic) {\n em, i, q {\n font-style: normal;\n }\n}<\/code><\/pre>\n\n\n\n
Every element is a style container<\/h3>\n\n\n
container-name<\/code> or
container-type<\/code> to define a style container because everything is a style container by default.<\/p>\n\n\n\n
style()<\/code> function. So, what container is being queried? It’s going to be the direct parent of the elements<\/strong> receiving the applied styles. And if not that, then it’s the next nearest relative container<\/strong> that takes precedence.<\/p>\n\n\n\n
size<\/code> and
inline-size<\/code>. Miriam explains it nicely:<\/p>\n\n\n\n
unknown<\/code> returned.<\/li><\/ul>\n\n\n\n
A container can support both dimensional and style queries<\/h3>\n\n\n
container-name<\/code>:<\/p>\n\n\n\n
@container style(font-style: italic) {\n em {\n font-style: normal;\n }\n}<\/code><\/pre>\n\n\n\n
container-type<\/code>. 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.<\/p>\n\n\n\n
container-type<\/code> 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<\/em> dimensional query:<\/p>\n\n\n\n
.card-container {\n container: card \/ inline-size; \/* implictly a style query container as well *\/\n}<\/code><\/pre>\n\n\n
Excluding a container from being queried<\/h3>\n\n\n
container-type: none<\/code> on an element.<\/p>\n\n\n\n
.some-element {\n container-type: none;\n}<\/code><\/pre>\n\n\n
Explicit style query containers offer more control of what gets queried<\/h3>\n\n\n
padding<\/code> , 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<\/code> is not an inherited property. <\/p>\n\n\n\n
container-name<\/code> 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:<\/p>\n\n\n\n
.card {\n container-name: card layout theme;\n}<\/code><\/pre>\n\n\n\n
container-name<\/code> accepts any number of optional and reusable<\/em> names for a container! That’s even more flexibility when it comes to helping the browser make a choice when searching for matches. <\/p>\n\n\n\n
.theme {\n container-name: theme;\n}\n.grid {\n container-name: layout;\n}\n.card {\n container-name: card layout theme;\n}<\/code><\/pre>\n\n\n\n
Style queries can be combined<\/h3>\n\n\n
or<\/code> and
and<\/code> operators allow us to combine wueries to keep things DRY:<\/p>\n\n\n\n
@container bubble style(--arrow-position: start start) or style(--arrow-position: end start) {\n .bubble::after {\n border-block-end-color: inherit;\n inset-block-end: 100%;\n }\n}\n\n\/* is the same as... *\/\n@container bubble style(--arrow-position: start start) {\n \/* etc. *\/\n}\n@container bubble style(--arrow-position: end start) {\n \/* etc. *\/\n}<\/code><\/pre>\n\n\n
Toggling styles<\/h3>\n\n\n
toggle()<\/code> function<\/a>. For example, we can cycle through two
font-style<\/code> values, say
italic<\/code> and
normal<\/code>:<\/p>\n\n\n\n
em, i, q {\n font-style: italic;\n}\n\n@container style(font-style: italic) {\n em, i, q {\n font-style: normal;\n }\n}<\/code><\/pre>\n\n\n\n
toggle()<\/code> function would be a simpler approach:<\/p>\n\n\n\n
em, i, q {\n font-style: toggle(italic, normal);\n}<\/code><\/pre>\n\n\n\n
toggle()<\/code> is less suitable. Style queries, though, are good to go. Miriam identifies three instances where style queries are more suitable than a
toggle()<\/code>:<\/p>\n\n\n\n
\/* When font-style is italic, apply background color. *\/\n\/* Toggles can only handle one property at a time. *\/\n@container style(font-style: italic) {\n em, i, q {\n background: lightpink;\n }\n}\n\n\/* When font-style is italic and --color-mode equals light *\/\n\/* Toggles can only evaluate one condition at a time *\/\n@container style((font-style: italic) and (--color-mode: light)) {\n em, i, q {\n background: lightpink;\n }\n}\n\n\/* Apply the same query condition to multiple properties *\/\n\/* Toggles have to set each one individually as separate toggles *\/\n@container style(font-style: italic) {\n em, i, q {\n \/* clipped gradient text *\/\n background: var(--feature-gradient);\n background-clip: text;\n box-decoration-break: clone;\n color: transparent;\n text-shadow: none;\n }\n}<\/code><\/pre>\n\n\n
Style queries solve the “Custom Property Toggle Hack”<\/h3>\n\n\n
--foo: ;<\/code>) and use the comma-separated fallback method to “toggle” properties on and off when then custom property is set to a real value.<\/p>\n\n\n\n
button {\n --is-raised: ; \/* off by default *\/\n \n border: 1px solid var(--is-raised, rgb(0 0 0 \/ 0.1));\n box-shadow: var(\n --is-raised,\n 0 1px hsl(0 0% 100% \/ 0.8) inset,\n 0 0.1em 0.1em -0.1em rgb(0 0 0 \/ 0.2)\n );\n text-shadow: var(--is-raised, 0 -1px 1px rgb(0 0 0 \/ 0.3));\n}\n\nbutton:active {\n box-shadow: var(--is-raised, 0 1px 0.2em black inset);\n}\n\n#foo {\n --is-raised: initial; \/* turned on, all fallbacks take effect. *\/\n}\n<\/code><\/pre>\n\n\n\n
Style queries and CSS generated content<\/h3>\n\n\n
content<\/code> property of
::before<\/code> and
::after<\/code> pseudo-elements, the matching container is the element on which the content is generated.<\/p>\n\n\n\n
.bubble {\n --arrow-position: end end;\n container: bubble;\n border: medium solid green;\n position: relative;\n}\n\n.bubble::after {\n content: \"\";\n border: 1em solid transparent;\n position: absolute;\n}\n\n@container bubble style(--arrow-position: end end) {\n .bubble::after {\n border-block-start-color: inherit;\n inset-block-start: 100%;\n inset-inline-end: 1em;\n }\n}<\/code><\/pre>\n\n\n
Style queries and web components<\/h3>\n\n\n
<template><\/code> of the component:<\/p>\n\n\n\n
<template id=\"media-host\">\n <article>\n <div part=\"img\">\n <slot name=\"img\">\u2026<\/slot>\n <\/div>\n <div part=\"content\">\n <slot name=\"title\">\u2026<\/slot>\n <slot name=\"content\">\u2026<\/slot>\n <\/div>\n <\/article>\n<\/template><\/code><\/pre>\n\n\n\n
:host<\/code> pseudo-element as a container to set a
container-name<\/code>, a
container-type<\/code>, and some high-level attributes on it:<\/p>\n\n\n\n
:host {\n container: media-host \/ inline-size;\n --media-location: before;\n --media-style: square;\n --theme: light;\n}<\/code><\/pre>\n\n\n\n
<media-host><\/code> can query the parameters of the
<media-host><\/code> element:<\/p>\n\n\n\n
@container media-host style(--media-style: round) {\n [part='img'] {\n border-radius: 100%;\n }\n}<\/code><\/pre>\n\n\n
What’s next?<\/h3>\n\n\n