{"id":2174,"date":"2024-05-21T11:34:09","date_gmt":"2024-05-21T17:34:09","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=2174"},"modified":"2024-05-23T14:35:28","modified_gmt":"2024-05-23T20:35:28","slug":"weve-got-container-queries-now-but-are-we-actually-using-them","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/weve-got-container-queries-now-but-are-we-actually-using-them\/","title":{"rendered":"We&#8217;ve Got Container Queries Now, But Are We Actually Using Them?"},"content":{"rendered":"\n<p>When container queries didn&#8217;t yet exist and CSS developers were clamoring for them, developers went so far as to claim:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>If we had container queries, <em>most<\/em> of what we write as media queries today would actually be container queries. <\/p>\n<\/blockquote>\n\n\n\n<p>Like, on any given project, there would be more <code>@container<\/code> in the CSS than <code>@media<\/code>. I joined that refrain. I thought for sure it would be true, if not, more like 75%, especially considering how so many sites these days are created by composing sites through component libraries, and since components don&#8217;t know where they will be used, the CSS that style those components would use <em>all<\/em> container queries.<\/p>\n\n\n\n<p>So, did this turn out to be true?<\/p>\n\n\n\n<p>Anecdotally: no, not really.<\/p>\n\n\n\n<p><a href=\"https:\/\/chromestatus.com\/metrics\/css\/timeline\/popularity\/699\">Measuring usage<\/a> backs that up tenfold, but that&#8217;s not fair since container queries are so relatively new, no matter how developers feel about them or use them on new projects, container queries will lag behind media queries for a very long time. Certainly the biggest two reasons for any lag in usage are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>It&#8217;s new.<\/strong> It takes time for education about features to make it out to developers. Even when you know, old habits die hard.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/caniuse.com\/css-container-queries\">Browser support<\/a> concern.<\/strong> The main one seems to be Safari 15, released as recently as 2022.<\/li>\n<\/ol>\n\n\n\n<p>But even on new or actively developed projects where the developers know about them and the browser support choices enable usage (speaking from personal experience here, and it is <a href=\"https:\/\/github.com\/GoogleChromeLabs\/container-query-polyfill\">polyfillable<\/a> for the record), it still feels like container queries aren&#8217;t reached for all that much. <\/p>\n\n\n\n<p>Why?!<\/p>\n\n\n\n<p>I have a few guesses:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Flexible layout methods that don&#8217;t need width\/inline-size based queries. The fact that with CSS grid we can use keywords like <code>auto-fit<\/code> and <code>auto-fill<\/code> to have <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/ZENWjdp\">an arbitrary number of columns<\/a> steals some of the need to specify columns at breakpoints. Also sometimes you can just&#8230; let stuff wrap.<\/li>\n\n\n\n<li>Fluid type. There are a number of ways to pull off fluid type nowdays, and that <a href=\"https:\/\/www.fluid-type-scale.com\/\">site-wide typography scaling<\/a> really helps scale sites without needing breakpoints.<\/li>\n\n\n\n<li>Components that tend to really benefit from styling updates at certain sizes are full-width anyway. Think of site header and footer elements that tend to be full width. For them, media and container queries are interchangeable.<\/li>\n<\/ol>\n\n\n\n<p>It&#8217;s the within-container layout shifts that seem like the most useful and common use case to me. My most common go-to example is essentially this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.card<\/span> {\n  <span class=\"hljs-attribute\">container<\/span>: card \/ inline-size;\n}\n<span class=\"hljs-selector-class\">.card<\/span> <span class=\"hljs-selector-class\">.grid<\/span> {\n  <span class=\"hljs-attribute\">display<\/span>: grid;\n  <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">2<\/span>fr;\n  <span class=\"hljs-attribute\">gap<\/span>: <span class=\"hljs-number\">1rem<\/span>;\n  @container (inline-size &lt; 300px) {\n    <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I&#8217;ve heard some interesting thoughts from others as well:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/mastodon.social\/@boldewyn\/112479650324376705\">\ud83d\udd17<\/a> Each container becomes a new stacking context which can cause <code>z-index<\/code> side effects that you might not want. <\/li>\n\n\n\n<li><a href=\"https:\/\/elk.zone\/social.lol\/@tylersticka\/112479697102678088\">\ud83d\udd17<\/a> It sometimes requires an additional wrapping element since you can&#8217;t style what you query.<\/li>\n\n\n\n<li><a href=\"https:\/\/sunny.garden\/@knowler\/112479729719514770\">\ud83d\udd17<\/a> CSS developers are only part of a whole. If the larger team doesn&#8217;t design around the idea they may not apply. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>It&#8217;s not that people <em>aren&#8217;t using them at all.<\/em> I&#8217;m using them more and more, myself. I <a href=\"https:\/\/elk.zone\/front-end.social\/@chriscoyier\/112400217715390354\">asked folks<\/a> recently if they have any examples, and got a few. Actually, the lack of responses is sort of what sparked this post.<\/p>\n\n\n\n<p>I figured I&#8217;d post some videos of recent usage on my own project here, not all of which is live yet. <\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 434px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='538' src='https:\/\/videopress.com\/embed\/SuEaYixe?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t<figcaption>Truncate &#8220;Line&#8221; to &#8220;Ln&#8221; and &#8220;Column&#8221; to &#8220;Col&#8221; when space constrained.<\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player aligncenter wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 556px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='403' src='https:\/\/videopress.com\/embed\/sQxrRsVz?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t<figcaption>Move some UI bits outside of their container to free up room for text in tight space constraints<\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player aligncenter wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 793px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='445' src='https:\/\/videopress.com\/embed\/LVvpWKl4?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t<figcaption>The most classic use case of all: the Card component.<\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player aligncenter wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 449px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='525' src='https:\/\/videopress.com\/embed\/GmvcFMnM?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t<figcaption>This is the combination of container-unit fluidity of the title text, and a container query breakpoint for the smaller text.<\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player aligncenter wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 795px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='274' src='https:\/\/videopress.com\/embed\/m95ir6M9?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t<figcaption>This is like a card component too, breaking into a single column when two columns are too tight. <\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t","protected":false},"excerpt":{"rendered":"<p>CSS developers got the ultimate dream: container queries. But now that they are here, are we actually reaching for them as much as we thought we would? <\/p>\n","protected":false},"author":1,"featured_media":2330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[47,7],"class_list":["post-2174","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-container-queries","tag-css"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/05\/pexels-photo-1544944.jpeg?fit=1880%2C1254&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=2174"}],"version-history":[{"count":11,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2174\/revisions"}],"predecessor-version":[{"id":2393,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2174\/revisions\/2393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/2330"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=2174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=2174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=2174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}