CSS propertyInteraction and more
scroll-behavior
Enables smooth scrolling when jumping to anchors and when scrolling via script.
- Initial value
auto- Inherited
- No
- Animatable
- no
- Applies to
- scroll containers
- JavaScript
element.style.scrollBehavior = "smooth"
Example
#CSS
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}Example HTML
<p style="margin: 0; color: #a3a3a3;">The whole page scrolls smoothly to anchors, but not for people who have turned off animations in their system.</p>Preview
Definition and usage
#The scroll-behavior: smooth property makes jumps to places on the page, for example after clicking a #section link, happen with a smooth animation instead of an instant jump.
It applies to scrolling triggered by navigation and by JavaScript, for example element.scrollIntoView(). It doesn’t change scrolling with the mouse wheel or a finger.
It is usually set on the html element. For people with the reduced-motion setting, it is worth turning smooth scrolling off in a prefers-reduced-motion query.
Syntax
#Syntax
scroll-behavior: auto | smooth;Values
#Values in practice
#auto
An instant jump after clicking the link.
.demo { scroll-behavior: auto; }smooth
Smooth scrolling to the target.
.demo { scroll-behavior: smooth; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- With a sticky header, add
scroll-margin-topto the targets so the header doesn’t cover them. - Turn smooth scrolling off for
prefers-reduced-motion: reduce.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.