CSS Reference ยท Scrolling, Interaction and UI
The scroll-behavior property
Reference for the CSS scroll-behavior property: turn on smooth scrolling for anchor links and script-driven scrolls, with a live demo, notes and motion tips.
The scroll-behavior property chooses how a scrolling box moves when something other than the user's own dragging or wheel input scrolls it. With smooth, the movement is animated over a short time; with auto, it jumps straight to the target.
It affects fragment navigation (clicking a link to #section), and scrolling started from JavaScript such as scrollTo() and scrollIntoView() when the script does not pass its own behavior option. It does not change how a mouse wheel, trackpad, keyboard or touch swipe scrolls.
html { scroll-behavior: smooth; }
Try it
Switch smooth scrolling on, then press the jump buttons: the box glides instead of snapping.
Syntax
scroll-behavior: auto | smooth;
- Initial value:
auto - Applies to: scrolling boxes (and the viewport, when set on the root element)
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
auto |
Scroll positions change instantly. |
smooth |
Scroll positions change with a browser-controlled animation. Duration and easing are chosen by the browser. |
Usage notes
-
To smooth the page scroll, put the property on
html(the root element). Setting it onbodyis not enough in most cases, because the viewport takes its value from the root. -
The value on the scrolling box matters, not on the element you scroll to.
-
Long distances can feel slow or disorienting for some people. Respect the reduced-motion setting:
CSS@media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }
-
A script can override the CSS:
el.scrollIntoView({ behavior: "instant" })skips the animation even when CSS sayssmooth. -
Smooth scrolling interacts well with
scroll-paddingandscroll-margin, which keep targets from hiding under a sticky header. -
Moving focus with a link or script can also trigger a smooth scroll, so keep the animation short in spirit: avoid huge pages with only a "back to top" link that takes many seconds.
Browser support
Supported in all current major browsers; it was added to some of them later than others, so very old versions simply scroll instantly, which is a safe fallback.
Related
scroll-paddingโ offset for the scrollportscroll-marginโ offset for a single targetscroll-snap-typeโ snap points- Lesson: Scroll Snap