{"id":7892,"date":"2025-12-03T11:22:36","date_gmt":"2025-12-03T16:22:36","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=7892"},"modified":"2025-12-03T11:22:37","modified_gmt":"2025-12-03T16:22:37","slug":"the-downsides-of-scrollbar-gutter-stable-and-one-weird-trick","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/the-downsides-of-scrollbar-gutter-stable-and-one-weird-trick\/","title":{"rendered":"The Downsides of scrollbar-gutter: stable; (and one weird trick)"},"content":{"rendered":"\n<p>The esteemed Zach (<a href=\"https:\/\/bsky.app\/profile\/strange.website\/post\/3m6hwyxrtk22q\">Leatherpants<\/a>), blogged about the virtues of <code>scrollbar-gutter: stable;<\/code>, suggesting it might be good in a starter stylesheet. (Hey, <a href=\"https:\/\/frontendmasters.com\/blog\/the-coyier-css-starter\/\">I&#8217;ve got one of those<\/a>.) The thing that it solves is content-shifting. If you have a page that, upon first render, doesn&#8217;t have a vertical scrollbar, but more content loads later and gets one, it can shift content horizontally to make room for the scrollbar, which is a smidge annoying. That&#8217;s assuming your OS setting has always visible scrollbars enabled. (I do in my macOS settings, I prefer it.)<\/p>\n\n\n\n<p>Also, navigating from a page that doesn&#8217;t require vertical scrolling to one that does (or vice versa) causes a layout shift to the tune of one scrollbar width.<\/p>\n\n\n\n<p>Using <code>scrollbar-gutter: stable;<\/code> (<a href=\"https:\/\/caniuse.com\/wf-scrollbar-gutter\">supported everywhere<\/a>) means that the browser will &#8220;reserve space&#8221; for a scrollbar and thus totally remove the layout-shifting downsides mentioned above.<\/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: 679px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='261' src='https:\/\/videopress.com\/embed\/8PQuzh4N?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=1739540970'><\/script><\/div>\n\t\t\t<figcaption>You can see the visual shift when we add content that overflows vertically. But if we flip on <code>scrollbar-gutter: stable;<\/code> the content stays steady when it goes back and forth between overflowing and not.<\/figcaption>\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>Notice in the video above though, the shifting-fix is accomplished by putting the space of the scrollbar there. You can see it&#8217;s quite literally a white bar. This only seems to happen when the page is rendered in an <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/bNprQEE\/1716173d165cddff9dd3489c2a64f645?editors=1100\"><code>&lt;iframe><\/code> like it is on CodePen<\/a>, but I still find it highly obnoxious and a downside (as there is no way I&#8217;ve found to make it not a white, or dark-in-dark-mode, bar). <\/p>\n\n\n\n<p>Here&#8217;s that demo:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_bNprQEE\" src=\"\/\/codepen.io\/anon\/embed\/bNprQEE?height=450&amp;theme-id=1&amp;slug-hash=bNprQEE&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed bNprQEE\" title=\"CodePen Embed bNprQEE\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Fortunately, the &#8220;literal white bar&#8221; problem isn&#8217;t there on regularly-rendered pages (outside of an iframe), as that would be an instant deal breaker. <\/p>\n\n\n\n<p>The remaining problem is centering.<\/p>\n\n\n\n<p>The space that is reserved for the maybe-maybe-not scrollbar cannot be factored into centering (like <code>margin: auto;<\/code> and whatnot). <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"656\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-02-at-6.36.38-PM.png?resize=1024%2C656&#038;ssl=1\" alt=\"Screenshot showing a content area with a gray background, below which there is a large white space indicating the absence of a vertical scrollbar, accompanied by red arrows pointing towards the white space.\" class=\"wp-image-7948\" style=\"width:404px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-02-at-6.36.38-PM.png?resize=1024%2C656&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-02-at-6.36.38-PM.png?resize=300%2C192&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-02-at-6.36.38-PM.png?resize=768%2C492&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-02-at-6.36.38-PM.png?w=1080&amp;ssl=1 1080w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">\ud83d\ude2d<\/figcaption><\/figure>\n<\/div>\n\n\n<p>So if you <em>really need<\/em> to visually center something, it&#8217;ll be fine-ish if there <em>is<\/em> a scrollbar, and noticeably not-centered-looking if there isn&#8217;t. Boooo.<\/p>\n\n\n\n<p>To me, this is just annoying enough to not put it in a starter stylesheet. <\/p>\n\n\n\n<p>But!<\/p>\n\n\n\n<p>Just for fun we could look at a newfangled CSS alternative. My big idea here is that we actually can tell if the page is overflowing and has a scrollbar or not these days. We can do this via <a href=\"https:\/\/caniuse.com\/wf-container-scroll-state-queries\">scroll-state queries<\/a>. <\/p>\n\n\n\n<p>So we make the whole page a scroll-state container then watch for when it is scrollable and push the whole page in a little bit the same width as the scrollbar. <\/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-tag\">html<\/span> {\n  <span class=\"hljs-attribute\">container-type<\/span>: scroll-state;\n  \n  <span class=\"hljs-attribute\">--scrollbar-width<\/span>: <span class=\"hljs-number\">25px<\/span>;\n  &amp;::--webkit-scrollbar {\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-built_in\">var<\/span>(--scrollbar-width);\n  }\n}\n\n<span class=\"hljs-selector-tag\">body<\/span> {\n  @container scroll-state(<span class=\"hljs-attribute\">scrollable<\/span>: block) {\n    padding-left: <span class=\"hljs-built_in\">var<\/span>(--scrollbar-width);\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>Notice I&#8217;m attempting to wrestle control over the width of the scrollbar there using those non-standard vendor prefixes. But 25px is the generally standard width of the scrollbar anyway. But that could change if a user set something like <code>scrollbar-width: thin;<\/code> or something. Makes me wish there was an <code>env()<\/code> variable or something that always reflects the width of the scrollbar at any DOM level. Anyway, if you have Chrome, you can see this approach working here:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_raeqaYg\" src=\"\/\/codepen.io\/anon\/embed\/raeqaYg?height=450&amp;theme-id=1&amp;slug-hash=raeqaYg&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed raeqaYg\" title=\"CodePen Embed raeqaYg\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Certainly the <code>scrollbar-gutter<\/code> approach is easier and far better supported, but it&#8217;s neat to know there are future options. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It maintains space for where a scrollbar would be, whether there actually is one or not. But do you always want that?<\/p>\n","protected":false},"author":1,"featured_media":7954,"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":[7,372,92],"class_list":["post-7892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-scroll-state","tag-scrolling"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/12\/scrollbar-gutter.jpg?fit=2000%2C1200&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/7892","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=7892"}],"version-history":[{"count":9,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/7892\/revisions"}],"predecessor-version":[{"id":7956,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/7892\/revisions\/7956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/7954"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=7892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=7892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=7892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}