Back when we released the v17 design (we’re on v18 now) of this site. I added html { scroll-behavior: smooth; }
to the CSS. Right away, I got comments like this (just one example):
… when you control+f or command+f and search on CSS-Tricks, it’ll scroll very slowly instead of snapping to the result, which makes finding information and keywords on CSS-Tricks much slower. As someone who uses this shortcut frequently, this is a usability issue for me.
Not terribly long after, I just removed it. I didn’t feel that strongly about it, and the fact that you have almost zero control over it, made me just can the idea.
I see it come up as a “CSS tip” a lot, so I chimed in with my experience:
After mentioning that, Christian Schaefer chimed in with a great idea:
Love that!
Christian blogged it:
Smooth scrolling is consequently applied to everything. Always. Even when cycling through the browser’s page search results. At least that’s the case for Chromium. So for the page search it would be desirable for the browser to make an exception to that rule and to deactivate smooth scrolling. Until the Chromium team fixes it, here is a trick how to solve the problem on your own with a little bit of extra CSS and HTML.
I’m not sure if Chrome (or any other browser) would consider that a bug or not. I doubt it’s specced since find-on-page isn’t really a web technology feature. But anyway, I much prefer find-on-page without it.
html:focus-within {
scroll-behavior: smooth;
}
It mostly works. The bummer part about it is situations like this…
<a href="#link-down-the-page">Jump down</a>
...
<h2 id="link-down-the-page">Header</h2>
That will jump the page down. With scroll-behavior: smooth;
in place, that’s kinda nice. But <h2>
is typically not a “focusable” element. So, with the trick above, there is now no focus within <html>
anymore, and the smooth scrolling is lost. If you want to preserve that, you’d have to do:
<h2 tabindex="-1" id="link-down-the-page">Header</h2>
I don’t think we should mess with scroll behavior. Some are sensitive to that smooth scroll and need those “steps”.
Appreciate your share though! Just sharing above. Smart fix!
It just occured to me that we updated our Smooth Scrolling snippet to make sure all headers that are scrolled to are also focuable:
Was kind of “controversial” when we did it, because of the somewhat unexpected visual focus styles.
Rally shouldn’t be controversial. When linking to an anchor, focuses should be moved, so it works for keyboard users as well.
Always add tabindex=”-1″ to non-interactive elements being linked to or receiving focus programmatically.
I highly recommend just adding that tabindex to the body. It also allows CSS focus targets that work well tapping anything else in the body on mobile.
Don’t forget to disable for folks who prefer reduced motion :)
Interesting. Firefox doesn’t have this issue.
Smooth scroll is helpful to a lot of users. Elderly users, specially, get confused when the screen just jumps.
This causes a problem with select elements using Firefox on macOS. You need to double click on the element to view the options.