CKTIS / JavaScript
HTMLElement.style.scrollBehavior — setting smooth scrolling
Read, set, or remove an element’s inline `scroll-behavior` declaration, and distinguish it from the resolved CSS value and a per-call scrolling option.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.style.scrollBehavior;
element.style.scrollBehavior = "smooth";
element.style.scrollBehavior = "auto";
element.style.scrollBehavior = "";HTMLElement.style.scrollBehavior is a CSSOM property on the style object of an HTML element instance, not a static property of the HTMLElement constructor or part of ECMAScript. Use element.style.scrollBehavior to read or change the inline CSS declaration for scroll-behavior.
Without an inline declaration, the getter returns "", even if a stylesheet sets the property. Assigning "" removes only that declaration; assigning "auto" sets it explicitly. On an element attached to the document, use getComputedStyle(element).scrollBehavior to inspect the resolved value after the cascade.
Setting this property does not start a scroll. It controls how a scroll container moves when navigation, such as a link to a page fragment, or a CSSOM scrolling API triggers a scroll. It does not change direct scrolling with a wheel or gesture. CSS auto is the initial value and means instant scrolling; smooth requests animation with timing and easing chosen by the browser. The property is not inherited by default, and browsers may ignore it.
To set the behavior of an HTML page’s viewport, use the root element (document.documentElement); a declaration on document.body does not carry over to the viewport. In element.scrollTo({ top: 100, behavior: "auto" }), "auto" defers to CSS; "smooth" and "instant" select the behavior for that call alone. "instant" is not a valid CSS scroll-behavior value. Avoid forcing smooth scrolling when prefers-reduced-motion: reduce matches.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inline scroll behavior</title>
</head>
<body>
<script>
const element = document.createElement("div");
console.log(JSON.stringify(element.style.scrollBehavior));
element.style.scrollBehavior = "smooth";
console.log(JSON.stringify(element.style.getPropertyValue("scroll-behavior")));
element.style.scrollBehavior = "";
console.log(JSON.stringify(element.style.scrollBehavior));
</script>
</body>
</html>Result
"" "smooth" ""
The example uses a detached element to show reading, setting, and clearing the declaration without moving the page. `JSON.stringify()` makes the empty string visible as `""`.
Related lessons
- JavaScript · Lesson 37 · Advanced · Version: 4