Don't forget about scroll-behavior: smooth! A small CSS property for a big UX improvement
scroll-behavior: smoothHave you ever clicked on an anchor link (like “Back to top”) and been instantly “teleported” to another part of the page? It”s fast, but very disorienting. The user loses context and doesn”t understand what happened.
Fortunately, there is an incredibly simple and elegant solution that will make your site more professional and enjoyable to use.
The name of this solution is
scroll-behavior: smooth.
scroll-behavior is a CSS property that determines how scrolling will behave when navigating through anchor links or when controlling the scroll via JavaScript.
By default, its value is auto, which means an instant “jump” to the desired location. But if you set the value to smooth, the browser will animate the scroll itself, creating a smooth and natural transition.
It couldn”t be simpler. Just add one line to your CSS file:
html {
scroll-behavior: smooth;
}And that”s it! Now any navigation through anchor links on your site will be smooth.
Imagine you have this markup:
HTML
<nav>
<a href="#section-1">Section 1</a>
<a href="#section-2">Section 2</a>
</nav>
<div id="section-1" style="height: 100vh; background: #f0f0f0;">
<h2>This is the first section</h2>
</div>
<div id="section-2" style="height: 100vh; background: #e0e0e0;">
<h2>This is the second section</h2>
</div>Without scroll-behavior: smooth, clicking on the “Section 2” link will instantly take you to the second block. With this property, the browser will smoothly scroll the page down.
In modern web development, the devil is in the details. scroll-behavior: smooth is one of those details that costs you almost no effort, but brings huge benefits.
Don”t ignore this feature. Add this line to your project right now and make navigating your site a little bit magical.