Ad space (banner)
🎨HTML & CSS Lessons
Lesson 42 / 50

Scrolling Smoothly with scroll-behavior

In this lesson you'll learn how to implement smooth scrolling using scroll-behavior, so you can improve the feel of an in-page link. This is for people searching "CSS scroll-behavior usage."

Specifying scroll-behavior: smooth makes clicking an in-page link (like #heading) scroll smoothly to its destination instead of jumping there instantly.

The sample code specifies html { scroll-behavior: smooth; }, and clicking the link "Go to section 2" smoothly scrolls to the position of the matching id="section2". Just adding this one line to the html element applies the effect to every in-page link on the site.

A common beginner stumbling block is not realizing this effect is achievable without JavaScript. In the past, animating a scroll with JavaScript was the common approach, but now the same effect can be achieved with a single line of CSS.

Adding just one line of CSS dramatically improves the feel of clicking a table-of-contents link. This is a commonly used finishing touch on long article pages, or single-page landing pages.

HTML & CSS
OUTPUT

💡 The preview runs entirely on this page — nothing is sent externally.

Ad space (banner)
Ad space (in-article)