Skip to main content

Command Palette

Search for a command to run...

Enable Smooth Scrolling With One Line of CSS

Published
โ€ข2 min read
Enable Smooth Scrolling With One Line of CSS
B

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐Ÿ˜€

In this article, I'll be showing you how to add smooth scrolling to your website with just one line of CSS.

But firstly, what is smooth scrolling?


What Is Smooth Scrolling?

Imagine a scenario when you have a button that navigates to a different section of your website. With smooth scrolling enabled, clicking that button will smoothly scroll to that section of the website rather than instantly snapping.

See it in action:

Hit the 'About Me' link on this website.

You will notice it smoothly scrolls down to the about section.

This effect is present on plenty of websites and is a useful arrow in your quiver so let's get into the code.


The CSS

To add smooth scrolling to your website simply add the following code to your CSS file:

html { scroll-behavior: smooth }

That's it. You have now added smooth scrolling to your website.

There is one caveat with this method that is its not supported by Safari (at least not on mobile) but other than that it is a great solution that doesn't involve any JavaScript.


Take Away

In this article, I've shown you how to add smooth scrolling to your website with just one line of CSS. Pretty cool right?

๐Ÿ‘Œ Thanks for reading this article!

If you like what I do and would love to see more related content, follow me on my other social platforms:

GitHub: Blake-K-Yeboah

LinkedIn: Blake Yeboah

Twitter: Blake Yeboah

You can also show your support by buying me a coffee ๐Ÿ˜ƒ

V

That's pretty cool. Is there a way to control the speed of the scroll? I also wonder if this can be done with transition in CSS. ๐Ÿค”

B

I don't think you can control the speed with this method. I believe you would need to implement some JavaScript to do that.

1

More from this blog

B

Blake Yeboah's Blog

41 posts

I write articles about React, JavaScript & Web Development to make your learning journey easier ๐Ÿ˜€