How to Create: Smooth Scrolling
- Previous Page Scroll Drawing
- Next Page Gradient Background Scrolling
Learn how to use CSS to create smooth scrolling effects.
Smooth Scrolling
Section 1
Click me to smoothly scroll to the bottom section 2.Click the link to see the 'smooth' scrolling effect.
Note:Removing the scroll-behavior attribute can cancel the smooth scrolling.
Smooth Scrolling
Add scroll-behavior: smooth
You can enable smooth scrolling for the entire page:
Note:You can also add it to a specific element/scroll container.
Example
html { scroll-behavior: smooth; }
Browser Support
The numbers in the table indicate the first browser version that fully supports the scroll-behavior property.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
61.0 | 79.0 | 36.0 | 14.0 | 48.0 |
cross-browser solutions
for browsers that do not support cross-browser solutions
for browsers that do not support jQuery)to create a solution that works across all browsers:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $("document").ready(function(){ // Add smooth scrolling to all links $("a").on('click', function(event) { // Ensure that this.hash has a value before overriding the default behavior if (this.hash !== "") { // Prevent the default anchor click behavior event.preventDefault(); // Store the hash value var hash = this.hash; // Add smooth page scrolling using jQuery's animate() method // Optional number (800) specifies the milliseconds needed to scroll to the specified area $('html, body').animate({ scrollTop: $(hash).offset().top }, 800, function(){ // After the scrolling is completed, add the hash (#) to the URL (default click behavior) window.location.hash = hash; ); } // End if ); ); </script>
Related Pages
Reference Manual:CSS scroll-behavior Property
- Previous Page Scroll Drawing
- Next Page Gradient Background Scrolling