Behold a horizontally-scrolling element:
.reel {
display: flex;
overflow-x: scroll;
}
This pattern is particularly useful on mobile devices as an alternative to stacking for breaking a grid apart, but its utility is not limited to small screens. A horizontally-scrolling section can also become the basis of an image slideshow on desktop browsers.
When people use the trackpad for navigating horizontally (often, with a two-finger gesture), we get an unintended side-effect: the gesture coincides with navigating through the browser history. A two-finger swipe to the right triggers the browser Back action, while two fingers to the left triggers the Forward action. When you reach the edges as you scroll the container, you often accidentally navigate away from the page (there's probably a name for the phenomenon).
To prevent the navigation, we can use the overscroll-behavior
CSS property. A value of none
or contain
will make sure the excess scroll does not ripple to the container's ancestors and, ultimately, the page:
.reel {
display: flex;
overflow-x: scroll;
overscroll-behavior-x: contain;
}
Here's a demo highlighting the difference.
At the time of writing, this property is supported in Firefox¹, Chrome², and Edge, so most desktop users will benefit from the enhanced behavior.
For extra credit also opt into smooth, accelerated scrolling in mobile Safari with -webkit-overflow-scrolling: touch
. While iOS / iPadOS 13 makes this the default, it's still a nice, ahem, touch for older versions.
¹ There's a small issue in Firefox with the first swipe
² I stumbled upon a peculiar combination that made Chrome ignore the declaration
If you liked this article, you may enjoy this simple trick for inputs in mobile Safari, or take a deep-dive into units for media queries.
Top comments (14)
Is there a demo? This CSS property doesn’t seem to be supported in Safari.
The property is only supported on Chrome, Firefox and Edge. I'll whip up a demo that shows the behavior.
I've updated the article with a link to the demo. Hope it helps!
Is this a standard feature on Windows laptops?
I have tested on a Microsoft Surface machine running Windows 10 and the gesture is enabled in Edge and Chrome (Firefox does not support it currently). I think the hardware needs to have a Precision Touchpad for this to work.
Thanks for checking. I tried on a regular Windows laptop, and there were no such gestures, at least not by default.
I also tried on my two Android phones, and swiping from the edges did not navigate in the browser for me. However, it seems to be an optional feature, according to Jason Miller, but I couldn’t find it.
Interesting! In either case, I would not expect
overscroll-behavior
to affect mobile navigation gestures, since they are separate from scrolling gestures, and much less likely to be performed accidentally.This CSS property doesn’t seem to be supported in Chrome 97.0.4692.71.
What operating system are you on? I don't have Chrome 97 specifically, but it works fine in Chrome 98 and 99 on macOS.
Windows 11, Chrome 128
overscroll-behavior-x: none
has no BACK-gesture prevention when scrolling the container horizontally via touchpad with two fingers. It still triggers BACK.Brilliant tip: thank you!
Thanks, Dave! Hope it's useful :-)
Damn handy article for today :3
Thanks, Dan!
Very Very useful! Easy to implement and has a noticeable impact in the perceived smoothness