
CSS Scroll Snap: Align Scrolling to Sections and Cards
05/05/2024
CSS Blend Modes: How to Mix Overlapping Colors
06/05/2024
HTML fragment links take readers directly to another part of a page. CSS scroll-behavior can animate that movement with no JavaScript or extra library.
1. Smooth scrolling between links and their targets
When someone follows a link to a fragment in the current document, the browser normally goes straight to the target. scroll-behavior: smooth asks it to scroll there gradually instead.
2. Syntax and values of scroll-behavior
Set the property on html for viewport scrolling, or on another scroll container for scrolling inside that container. It affects navigation and CSSOM scrolling; it does not change ordinary wheel or touch scrolling.
Syntax:
html{
scroll-behavior: auto | smooth;
}
- auto: The default. Scrolling initiated in these ways is instant.
- smooth: Requests animated scrolling. The browser controls its timing and can ignore the request.
3. Browser support
Check browser support for scroll-behavior for your project. Fragment links still work when a browser ignores the property. Respect reduced-motion preferences when enabling smooth scrolling.
4. Differences between scroll-behavior and Scroll Snap
scroll-behavior controls how a navigation or programmatic scroll moves. Scroll Snap controls where a scroll position can settle within a scrolling container.
You can use the two together. See the CSS Scroll Snap lesson for examples of alignment along both axes.
5. Practical scroll-behavior examples
The examples show three ways to navigate to another part of the same page.
5.1. Navigation links to page sections
A landing page can use a navigation menu whose links point to sections with distinct IDs.
HTML:
<nav>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
</ul>
</nav>
<section id="section1">Section 1</section>
<section id="section2">Section 2</section>
<section id="section3">Section 3</section>
<section id="section4">Section 4</section>
CSS:
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
Try it in CodePen:
5.2. Back-to-top link
The link at the bottom targets an element near the top. You can also keep such a link visible along the edge of a long page if it does not obscure the content.
HTML:
<div class="container" id="scrollBtn"> <p>Scroll to the end of the page and follow the Back to top link.</p> </div> <a href="#scrollBtn">↑ Back to top</a>
CSS:
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
Try it in CodePen:
5.3. Table of contents in a sidebar
The sidebar shows a list of links on the left, while the article sections are on the right. Each link points to a different section.
HTML:
<div class="container">
<aside>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
</ul>
</aside>
<main>
<section id="section1">
<h2>Section 1</h2>
<p>...</p>
</section>
<section id="section2">
<h2>Section 2</h2>
<p>...</p>
</section>
<section id="section3">
<h2>Section 3</h2>
<p>...</p>
</section>
<section id="section4">
<h2>Section 4</h2>
<p>...</p>
</section>
</main>
</div>
CSS:
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
Try it in CodePen:
Try the examples with the keyboard and your system’s reduced-motion preference. Find more articles through the Eniun newsletter.