{"id":2985,"date":"2024-07-10T16:09:38","date_gmt":"2024-07-10T22:09:38","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=2985"},"modified":"2024-07-10T16:09:39","modified_gmt":"2024-07-10T22:09:39","slug":"single-directionally-allowed-overflow","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/single-directionally-allowed-overflow\/","title":{"rendered":"Single-Directionally Allowed Overflow"},"content":{"rendered":"\n<p>There is this annoying thing in CSS where it <em>feels<\/em> like you should be able to <strong>hide<\/strong> <code>overflow<\/code> in one direction and <strong>allow<\/strong> it in another, since they can be separate properties:<\/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\">.nav-bar<\/span> {\n  <span class=\"hljs-attribute\">overflow-x<\/span>: hidden;\n  <span class=\"hljs-attribute\">overflow-y<\/span>: visible;\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>But <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/jOjExOj\">you&#8217;ll be disappointed<\/a>.<\/p>\n\n\n\n<p>I really wish I could explain to you <em>why<\/em> you can&#8217;t do that, but I don&#8217;t know the historical CSS discussions that got us here. People clearly want to do it sometimes. I know, I&#8217;ve read <em>tons<\/em> of threads about it. The most common use case is something like an app sidebar along the left of a layout which can scroll vertically, but allows for menus that can extend out of it to the right. Another use case is a header bar on top of a site that hides the horizontal overflow but allows for vertical (sometimes a nice protection to avoid awkward &#8220;page zoom outs&#8221; with content that accidentally overflows).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Good news: you can do it with the <code>clip<\/code> value<\/h2>\n\n\n\n<p>This <em>does<\/em> work:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-selector-class\">.nav-bar<\/span> {\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-attribute\">overflow-x<\/span>: clip;\n<\/span><\/mark><span class='shcb-loc'><span>  <span class=\"hljs-attribute\">overflow-y<\/span>: visible;\n<\/span><\/span><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>Here&#8217;s the proof:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_eYaqGXm\" src=\"\/\/codepen.io\/anon\/embed\/eYaqGXm?height=450&amp;theme-id=47434&amp;slug-hash=eYaqGXm&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed eYaqGXm\" title=\"CodePen Embed eYaqGXm\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Caveats<\/h2>\n\n\n\n<p><a href=\"https:\/\/caniuse.com\/mdn-css_types_overflow_clip\">Support is mostly fine<\/a>, unless you worry about Safari 15. <\/p>\n\n\n\n<p>Also, there is a difference between the values <code>hidden<\/code> and <code>clip<\/code>. <\/p>\n\n\n\n<p>The <code>hidden<\/code> value does visually hide the overflow and will not add any visible scrollbars to the element. But! The <code>hidden<\/code> value does still technically allow you to scroll that element. You can force it sometimes with a mouse by highlighting text and dragging the direction of where the overflow is. And you can do it with JavaScript. The <code>clip<\/code> value does <em>not<\/em> allow you to do this. The content that is &#8220;clipped&#8221; away is truly inaccessible (visually). <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Kilian Valkhof <a href=\"https:\/\/kilianvalkhof.com\/2022\/css-html\/do-you-know-about-overflow-clip\/\">had a nice article about all this<\/a> a while back, and also shows off the related <code>overflow-clip-margin<\/code> which is a nice bonus feature to clipping. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS allows setting different overflow behaviors for x and y directions. It doesn&#8217;t actually work with the `hidden` value, but it does with `clip`!<\/p>\n","protected":false},"author":1,"featured_media":2993,"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,202],"class_list":["post-2985","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-overflow"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-10-at-3.07.39%E2%80%AFPM.png?fit=1754%2C1156&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2985","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=2985"}],"version-history":[{"count":9,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2985\/revisions"}],"predecessor-version":[{"id":2995,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2985\/revisions\/2995"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/2993"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=2985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=2985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=2985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}