The CSS scroll-behavior property controls how scrolling happens when a user clicks anchor links, uses keyboard navigation, or triggers scrolling through JavaScript. It lets you enable smooth, animated scrolling without JavaScript.
Note
β Works on the document (via html) and individual scroll containers
β Doesnβt affect mouse wheel scrollingβonly programmatic or anchor scrolling
π¦ Syntax
scroll-behavior syntax
scroll-behavior: auto | smooth;| Value | Description |
|---|---|
| auto | Instant jump (default behavior) |
| smooth | Smooth animated scrolling |
β¨ 1. Enable Smooth Scrolling Website-Wide
Apply to html to affect the whole page.
Global smooth scroll
html {
scroll-behavior: smooth;
}Note
β¨ 2. Smooth Scrolling Inside a Container
Works on any element that has scrolling.
Container scroll behavior
.scroll-box {
overflow-y: auto;
scroll-behavior: smooth;
}β¨ 3. Smooth Scrolling + Anchor Links
When a user clicks a link like<a href="#section3">Go to Section 3</a>the scroll animation becomes smooth.
Anchor example
html {
scroll-behavior: smooth;
}β¨ 4. Works With JavaScript Scrolling Too
JS scrolling with smooth behavior
document.querySelector('#box').scrollIntoView(); // scrolls smoothlyNote
π 5. Per-Element Smooth Scroll
This allows your UI components to have different scroll behaviors.
Different elements, different scroll-behavior
.modal {
scroll-behavior: smooth;
}
.sidebar {
scroll-behavior: auto;
}π 6. Scroll Snap + Scroll Behavior
Combining scroll snapping with smooth scrolling results in a clean slider/carousel feel.
Smooth scroll + snap
.carousel {
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
}π§ͺ Real-World Use Cases
1οΈβ£ Single-Page Navigation
Navbar anchors
html { scroll-behavior: smooth; }2οΈβ£ Chat Apps
Chat autoscroll
messages.scrollTo({ top: messages.scrollHeight })3οΈβ£ Animated Carousels
Horizontal smooth scroll
.slider { scroll-behavior: smooth; }β οΈ Common Mistakes
- β Expecting it to animate mouse-wheel or touch scrolling (it doesnβt)
- β Applying it to non-scrollable elements
- β Forgetting scroll container must have overflow enabled
- β Assuming scroll-behavior works in iframes (often restricted)
π₯ Summary
The scroll-behavior property enables smooth, native scrolling with zero JavaScript. It's perfect for modern navigation, sliders, modals, and scrollable UIs.