CSS scroll-behavior Property
The CSS scroll-behavior property controls whether scrolling initiated by navigation or scrolling APIs happens instantly or uses a smooth animation.
It is commonly used with anchor links, in-page navigation, and scrollable containers. The property affects scrolling performed by the browser or through supported APIs, but it does not control the behavior of ordinary scrolling performed directly by the user.
scroll-behavior Syntax
The scroll-behavior property accepts two keywords: auto and smooth.
Basic Syntax
selector {
scroll-behavior: smooth;
}
Available Values
.scroll-container {
scroll-behavior: auto;
}
.scroll-container {
scroll-behavior: smooth;
}
CSS-Wide Keywords
.scroll-container {
scroll-behavior: initial;
}
.scroll-container {
scroll-behavior: inherit;
}
.scroll-container {
scroll-behavior: unset;
}
.scroll-container {
scroll-behavior: revert;
}
.scroll-container {
scroll-behavior: revert-layer;
}
The property does not accept time values, easing functions, lengths, or percentages. The browser determines the duration and timing of smooth scrolling.
Property Values
| Value | Description |
|---|---|
auto |
Uses the browser's normal scrolling behavior. This is the initial value and generally results in an immediate scroll. |
smooth |
Requests an animated scroll between the current position and the destination. The browser controls its timing. |
initial |
Resets the property to auto. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to auto because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Scrolling boxes |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
Valid Values
html {
scroll-behavior: smooth;
}
.scroll-container {
scroll-behavior: auto;
}
.scroll-container {
scroll-behavior: smooth;
}
Invalid Values
html {
scroll-behavior: instant;
}
html {
scroll-behavior: 500ms;
}
html {
scroll-behavior: ease-in-out;
}
html {
scroll-behavior: smooth 1s;
}
html {
scroll-behavior: 50%;
}
Only auto, smooth, and CSS-wide keywords are valid. Although some JavaScript scrolling APIs accept an instant behavior, instant is not a valid CSS scroll-behavior value.
How It Works
When a browser scrolls to a destination through an anchor link or a supported scrolling API, it can move directly to the destination or animate the movement.
The scroll-behavior property requests which behavior should be used for that scrolling box.
Default Scrolling
html {
scroll-behavior: auto;
}
Navigation to an anchor normally moves to the destination immediately.
Smooth Scrolling
html {
scroll-behavior: smooth;
}
Navigation to an anchor requests an animated movement to the destination.
Applying the Property to the Document
For ordinary document scrolling, apply scroll-behavior to the root <html> element.
html {
scroll-behavior: smooth;
}
Applying the Property to a Container
.scroll-panel {
height: 250px;
overflow-y: auto;
scroll-behavior: smooth;
}
The property affects scrolling within the panel rather than the entire document.
Browser-Controlled Timing
The browser determines the duration and easing of smooth scrolling. CSS does not provide a way to set a specific scrolling duration through this property.
User-Initiated Scrolling
Direct scrolling with a mouse wheel, touch gesture, or keyboard is not controlled by this property. Browsers handle those interactions according to their own scrolling behavior.
Important Distinction
The scroll-behavior property changes how a supported scroll operation moves to its destination. It does not change the destination itself.
auto Value
The auto value uses the browser's normal scrolling behavior and is the initial value of scroll-behavior.
CSS Example
html {
scroll-behavior: auto;
}
How auto Works
When an anchor link is activated, the browser generally moves directly to the destination without an animated transition.
Using auto in a Scroll Container
.scroll-panel {
overflow-y: auto;
scroll-behavior: auto;
}
Programmatic scrolling within the panel follows the browser's normal scrolling behavior.
When to Use auto
Use auto when immediate navigation is preferred or when animated movement would interfere with usability.
Important Consideration
The auto value does not guarantee that every browser or platform will scroll in exactly the same way. The browser retains control over its normal scrolling behavior.
smooth Value
The smooth value requests an animated transition from the current scroll position to the destination.
CSS Example
html {
scroll-behavior: smooth;
}
How smooth Works
Instead of jumping directly to the destination, the browser scrolls through intermediate positions until the target is reached.
Smooth Scrolling Within a Panel
.scroll-panel {
max-height: 300px;
overflow-y: auto;
scroll-behavior: smooth;
}
When to Use smooth
Smooth scrolling can help users understand movement between sections of a long page or within a scrollable interface.
Scroll Duration
The browser controls how long the animation takes. A declaration such as scroll-behavior: smooth 500ms is invalid.
Browser Preferences
Browsers may ignore smooth scrolling requests or adjust their behavior according to platform settings and user preferences.
Important Consideration
Long-distance smooth scrolling can cause discomfort for some users. Provide a reduced-motion alternative and avoid unnecessary animated navigation.
Smooth Anchor Navigation
One of the most common uses of scroll-behavior is smooth navigation between sections of a page.
HTML Example
<nav aria-label="Page sections">
<a href="#introduction">Introduction</a>
<a href="#examples">Examples</a>
<a href="#summary">Summary</a>
</nav>
<section id="introduction">
<h2>Introduction</h2>
<p>Introduction content goes here.</p>
</section>
<section id="examples">
<h2>Examples</h2>
<p>Example content goes here.</p>
</section>
<section id="summary">
<h2>Summary</h2>
<p>Summary content goes here.</p>
</section>
CSS Example
html {
scroll-behavior: smooth;
}
Clicking an anchor link requests smooth scrolling to the element with the matching id.
Working With Sticky Headers
A fixed or sticky header can cover the heading at an anchor destination. Use scroll-padding-top on the scrolling container to reserve space for the header.
html {
scroll-behavior: smooth;
scroll-padding-top: 80px;
}
Adjusting Individual Targets
section[id] {
scroll-margin-top: 80px;
}
The scroll-margin-top property adds an offset around individual targets when the browser calculates their scroll positions.
Important Consideration
Anchor links work without JavaScript. Smooth scrolling is a visual enhancement, so the links should remain functional when the property is unsupported or disabled.
Smooth Scrolling in Containers
The scroll-behavior property also works with elements that create their own scrollable areas.
Creating a Scroll Container
.scroll-panel {
height: 250px;
overflow-y: auto;
scroll-behavior: smooth;
}
The fixed height and overflowing content create a vertical scrolling area.
Horizontal Scroll Container
.horizontal-panel {
display: flex;
gap: 20px;
overflow-x: auto;
scroll-behavior: smooth;
}
The container can scroll horizontally when its content exceeds the available width.
Combining With Scroll Padding
.scroll-panel {
height: 250px;
overflow-y: auto;
scroll-behavior: smooth;
scroll-padding-top: 20px;
}
Scroll padding provides additional space between the scrolling container's edge and the intended target position.
Combining With Scroll Margin
.scroll-panel .target {
scroll-margin-top: 20px;
}
Scroll margin adjusts the target's scroll alignment area.
Important Consideration
The scroll-behavior property does not create a scroll container. The element must already be scrollable for the property to have a visible effect.
JavaScript Scrolling
JavaScript provides scrolling methods that can use the behavior established by CSS or request a specific behavior for an individual operation.
Using scrollIntoView()
document.querySelector("#examples").scrollIntoView();
When the scrolling behavior is not explicitly overridden, the relevant scrolling containers can use their CSS scroll-behavior settings.
Requesting Smooth Scrolling
document.querySelector("#examples").scrollIntoView({
behavior: "smooth"
});
This requests smooth scrolling directly through JavaScript.
Using scrollTo()
window.scrollTo({
top: 500,
behavior: "smooth"
});
This requests smooth scrolling to a vertical position of 500 pixels.
Using CSS-Defined Behavior
html {
scroll-behavior: smooth;
}
window.scrollTo({
top: 500,
behavior: "auto"
});
For the JavaScript scrolling APIs, behavior: "auto" uses the scrolling box's computed scroll-behavior value.
Important Consideration
A JavaScript request using behavior: "smooth" can request smooth scrolling independently of a CSS declaration. If motion must be reduced, account for both CSS and JavaScript scrolling behavior.
scroll-behavior Versus Scroll Snap
The scroll-behavior property and CSS Scroll Snap control different aspects of scrolling.
| Property | Purpose |
|---|---|
scroll-behavior |
Controls whether supported scrolling operations use smooth or normal scrolling behavior. |
scroll-snap-type |
Defines whether and how a scrolling container snaps to designated positions. |
scroll-snap-align |
Defines the alignment positions of individual snap targets. |
scroll-padding |
Defines offsets within a scroll container's optimal viewing region. |
scroll-margin |
Defines offsets around elements used as scroll targets. |
Combining Smooth Scrolling and Scroll Snap
.scroll-gallery {
display: flex;
overflow-x: auto;
scroll-behavior: smooth;
scroll-snap-type: x mandatory;
}
.scroll-gallery .slide {
flex: 0 0 100%;
scroll-snap-align: start;
}
The container can request smooth programmatic scrolling while also snapping to designated slide positions.
Important Distinction
Scroll Snap determines eligible stopping positions. The scroll-behavior property controls the animation behavior of supported scroll operations.
scroll-behavior Example
The following example uses anchor links inside a scrollable container to demonstrate smooth scrolling without changing the scroll behavior of the entire page.
HTML Example
<div class="smooth-scroll-demo">
<nav aria-label="Scroll example sections">
<a href="#demo-section-one">Section One</a>
<a href="#demo-section-two">Section Two</a>
<a href="#demo-section-three">Section Three</a>
</nav>
<div class="scroll-panel">
<section id="demo-section-one">
<h3>Section One</h3>
<p>This is the first section.</p>
</section>
<section id="demo-section-two">
<h3>Section Two</h3>
<p>This is the second section.</p>
</section>
<section id="demo-section-three">
<h3>Section Three</h3>
<p>This is the third section.</p>
</section>
</div>
</div>
CSS Example
.smooth-scroll-demo {
max-width: 600px;
}
.smooth-scroll-demo nav {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 15px;
}
.smooth-scroll-demo nav a {
padding: 8px 12px;
background-color: #2563eb;
color: #ffffff;
text-decoration: none;
}
.smooth-scroll-demo .scroll-panel {
height: 200px;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #cbd5e1;
}
.smooth-scroll-demo .scroll-panel section {
min-height: 200px;
padding: 20px;
box-sizing: border-box;
scroll-margin-top: 0;
}
.smooth-scroll-demo .scroll-panel section:nth-child(odd) {
background-color: #eff6ff;
}
.smooth-scroll-demo .scroll-panel section:nth-child(even) {
background-color: #dbeafe;
}
@media (prefers-reduced-motion: reduce) {
.smooth-scroll-demo .scroll-panel {
scroll-behavior: auto;
}
}
Section One
This is the first section.
Use the navigation links to move between sections.
Section Two
This is the second section.
The scroll container requests smooth movement.
Section Three
This is the third section.
Use the links above to return to another section.
Click the navigation links to move between the sections. The scrollable panel requests smooth scrolling while the rest of the page retains its normal scrolling behavior.
Switching to Normal Scrolling
.smooth-scroll-demo .scroll-panel {
scroll-behavior: auto;
}
This changes the container back to its normal scrolling behavior.
Increasing the Container Height
.smooth-scroll-demo .scroll-panel {
height: 300px;
}
This increases the visible area of the scroll container without changing its scrolling behavior.
Browser Compatibility Note
Browsers may use different smooth scrolling durations or ignore smooth scrolling requests based on user preferences. The anchor links remain functional when smooth scrolling is unavailable.
Accessibility Considerations
Smooth scrolling can help users follow navigation changes, but animated movement may cause discomfort for people with motion sensitivities.
Respect Reduced Motion Preferences
Use the prefers-reduced-motion media query to disable smooth scrolling for users who request reduced motion.
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}
Preserve Functional Navigation
Use ordinary anchor links for navigation so destinations remain accessible when CSS is unavailable.
Maintain Keyboard Accessibility
Navigation links must remain keyboard accessible and display a visible focus indicator.
Consider Focus Management
Scrolling to an element does not always mean that keyboard focus moves to it. When building scripted navigation, manage focus appropriately so keyboard and assistive technology users understand the destination.
Avoid Excessive Motion
Long scrolling animations can be disorienting. Avoid forcing users through unnecessary movement between distant sections.
Account for Sticky Headers
Use scroll padding or scroll margin to prevent target headings from being hidden behind fixed navigation.
Maintain Readable Content
Smooth scrolling should not interfere with reading, zooming, or navigating through content.
Important Consideration
Smooth scrolling is an enhancement, not a requirement for functional navigation. The destination should remain reachable even when animation is disabled.
Common Mistakes
Using an Invalid Duration
html {
scroll-behavior: smooth 500ms;
}
The property does not accept a duration. The browser controls smooth scrolling timing.
Using instant as a CSS Value
html {
scroll-behavior: instant;
}
The CSS property accepts auto or smooth, not instant.
Applying the Property to body for Document Scrolling
body {
scroll-behavior: smooth;
}
For document viewport scrolling, use the root html element. The property on body is not propagated to the viewport.
Expecting Mouse Wheel Scrolling to Change
The property does not control ordinary user scrolling with a mouse wheel, touch gesture, or keyboard.
Applying It to a Non-Scrollable Element
.panel {
scroll-behavior: smooth;
}
Without a scrollable area or applicable scrolling operation, the property may have no visible effect.
Confusing scroll-behavior With Scroll Snap
The property controls scrolling behavior, not the positions where scrolling should stop.
Ignoring Reduced Motion Preferences
Always consider users who prefer reduced motion when enabling smooth scrolling.
Expecting Identical Animation Timing
Browsers control smooth scrolling duration and easing, so the movement can differ between implementations.
Ignoring Sticky Headers
Anchor targets can become hidden behind fixed or sticky headers. Use scroll-padding or scroll-margin when appropriate.
Important Considerations
Keep the following points in mind when using scroll-behavior:
- It controls the behavior of supported scrolling operations.
- Its initial value is
auto. - The property is not inherited.
- It applies to scrolling boxes.
autouses the browser's normal scrolling behavior.smoothrequests an animated movement to the destination.- The browser determines smooth scrolling duration and easing.
- The property does not accept time values.
- The property does not accept easing functions.
instantis not a valid CSS value.- For document viewport scrolling, apply it to the root
htmlelement. - Applying it to
bodydoes not propagate the value to the viewport. - It can be used on independently scrollable containers.
- It does not create scrolling overflow.
- It does not control ordinary user scrolling with a mouse wheel or touch gesture.
- It works with anchor navigation and supported scrolling APIs.
- JavaScript can request smooth scrolling independently.
- Browsers may ignore smooth scrolling requests.
- It does not determine scroll destinations.
- Scroll Snap controls designated stopping positions.
- Scroll padding adjusts the optimal viewing region within a scrolling container.
- Scroll margin adjusts the target area used for scrolling alignment.
- Sticky headers can obscure anchor destinations.
- Use
prefers-reduced-motionto respect motion preferences. - Long-distance animated scrolling can be uncomfortable for some users.
- Anchor links should remain functional without smooth scrolling.
- Keyboard navigation and focus indicators must remain accessible.
- Scripted scrolling may require additional focus management.
- Test scrolling behavior across browsers and input methods.
Browser Support
The CSS scroll-behavior property is Baseline Widely available and is supported in current versions of major browsers.
Smooth scrolling timing and behavior are controlled by the browser. Browsers may ignore smooth scrolling requests or modify their behavior according to user preferences and platform settings.
For current compatibility information, visit Can I Use? and search for scroll-behavior.
Anchor navigation and scrolling remain functional when smooth scrolling is unsupported, although the movement may occur immediately.
Specifications
The scroll-behavior property is defined in CSS Overflow Module Level 3.
The specification defines the auto and smooth values and their behavior for scrolling boxes.
For additional documentation and examples, consult MDN Web Docs.
