Enable smooth scrolling behavior
Quick Answer
Add `scroll-behavior: smooth` to the `html` selector to make all anchor link jumps animate smoothly instead of snapping.
1html {
2 scroll-behavior: smooth;
3}
4
5/* Hide scrollbar but keep functionality */
6.hide-scrollbar {
7 -ms-overflow-style: none;
8 scrollbar-width: none;
9}
10
11.hide-scrollbar::-webkit-scrollbar {
12 display: none;
13}Setting scroll-behavior: smooth on the html element makes all anchor link jumps animate smoothly instead of snapping instantly. The hide-scrollbar utility removes the visible scrollbar track without disabling scrolling, which is useful for custom-designed scroll containers.
Yes, `scroll-behavior: smooth` affects `window.scrollTo()`, `element.scrollIntoView()`, and `element.scrollTop` assignments. You can also pass `{ behavior: 'smooth' }` directly to these methods for per-call control.
This free css code snippet for smooth scrolling is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement smooth scrolling quickly and correctly.
All snippets in the Snippetly library follow css best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses css.
Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.