JavaScript Document scrollingElement Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.scrollingElement is a read-only instance property that returns the Element which scrolls the document. Learn standards vs quirks mode, scroll to top with scrollTop, compare with documentElement and body, and five examples with try-it labs.

01

Kind

Read-only property

02

Returns

Element | null

03

Standard

documentElement

04

Scroll

scrollTop / scrollLeft

05

Quirks

body or null

06

Status

Baseline widely

Introduction

When users scroll a long page, something in the DOM holds the scroll position. Before scrollingElement, developers often guessed between document.documentElement and document.body. The standard property tells you which element actually scrolls.

MDN: the scrollingElement read-only property returns a reference to the Element that scrolls the document. In standards mode, this is the root element of the document, document.documentElement.

💡
Use it for reliable page scroll

MDN’s example sets scrollTop = 0 on document.scrollingElement to jump to the top. That works across browsers without hard-coding <html> vs <body>.

Related Document tutorials: documentElement, body, compatMode, defaultView.

Understanding Document.scrollingElement

A read-only instance property from the CSSOM View Module.

  • Standards mode — returns root element (documentElement, MDN).
  • Quirks mode — returns body if it exists and is not potentially scrollable; otherwise null (MDN).
  • Scroll props — use scrollTop and scrollLeft on the returned Element.
  • Alternativewindow.scrollTo() for the same visual result.
  • Status — Baseline Widely available (since August 2016, MDN).

📝 Syntax

JavaScript
document.scrollingElement

Value

The Element that scrolls the document, usually the root element (unless not in standard mode, MDN).

MDN scroll-to-top example

JavaScript
const scrollElm = document.scrollingElement;
scrollElm.scrollTop = 0;

⚡ Quick Reference

GoalCode / note
Get scrolling rootdocument.scrollingElement
Scroll to topdocument.scrollingElement.scrollTop = 0
Read scroll positiondocument.scrollingElement.scrollTop
Horizontal scrolldocument.scrollingElement.scrollLeft
Standards modeUsually same as documentElement (MDN)
Alternative APIwindow.scrollTo(0, 0)

🔍 At a Glance

Four facts about document.scrollingElement.

Type
Element|null

Read-only

Standard
documentElement

MDN

Scroll
scrollTop

Position

Status
Baseline

Since 2016

📋 scrollingElement vs documentElement

scrollingElementdocumentElement
PurposeElement that scrolls the documentRoot element of the document
Standards modeSame node (MDN)<html> root
Quirks modebody or null (MDN)Still <html>
Best for scroll positionYesWorks in standards mode only

Examples Gallery

Examples follow MDN Document: scrollingElement. Each includes a try-it lab you can run in the browser.

📚 Getting Started

Read the scrolling root on a modern HTML page.

Example 1 — Read the Scrolling Element

Log which element scrolls the document in standards mode.

JavaScript
const scrollElm = document.scrollingElement;
console.log(scrollElm.tagName); // "HTML" in standards mode
Try It Yourself

How It Works

MDN: in standards mode, scrollingElement is the document root (<html>).

Example 2 — Scroll to Top (MDN)

Set scrollTop = 0 on the scrolling element.

JavaScript
const scrollElm = document.scrollingElement;
scrollElm.scrollTop = 0;
Try It Yourself

How It Works

This is MDN’s official example pattern for resetting vertical scroll.

📈 Practical Patterns

Compare, measure, and wrap scroll logic.

Example 3 — Compare with documentElement

Check whether both properties reference the same node.

JavaScript
const same =
  document.scrollingElement === document.documentElement;

console.log("Same node in standards mode?", same);
Try It Yourself

How It Works

On a normal page with <!DOCTYPE html>, both point at <html>.

Example 4 — Read Current Scroll Position

Read scrollTop to know how far the user has scrolled.

JavaScript
const y = document.scrollingElement.scrollTop;
console.log("Vertical scroll:", y, "px");
Try It Yourself

How It Works

Use this for sticky headers, scroll-spy navigation, or “back to top” button visibility.

Example 5 — Safe Scroll Helper

Wrap scroll logic with a null check for edge cases.

JavaScript
function scrollToTop() {
  const el = document.scrollingElement;
  if (el) {
    el.scrollTop = 0;
    el.scrollLeft = 0;
  }
}

scrollToTop();
console.log("Scrolled to top");
Try It Yourself

How It Works

MDN notes quirks-mode edge cases where scrollingElement can be null; a guard keeps code safe.

🚀 Common Use Cases

  • Back to top buttons — set scrollTop = 0 on click.
  • Route changes in SPAs — reset scroll after navigation.
  • Scroll-spy menus — read scrollTop to highlight sections.
  • Infinite scroll — detect when user nears the bottom.
  • Modal open/close — save and restore scroll position.
  • Cross-browser scroll code — avoid guessing html vs body.

🧠 How document.scrollingElement Works

1

Browser picks document mode

Standards mode (CSS1Compat) vs quirks mode (BackCompat).

compatMode
2

Document exposes scrollingElement

Returns the Element that owns page scroll (MDN).

Property
3

You read or set scrollTop / scrollLeft

Measure position or jump to top, left, or any offset.

Scroll
4

One reliable scroll root

No more hard-coding documentElement vs body in modern pages.

📝 Notes

  • MDN: Baseline Widely available (since August 2016) — no Deprecated / Experimental / Non-standard banner.
  • In standards mode, usually identical to document.documentElement (MDN).
  • Quirks mode: may return body or null (MDN) — use <!DOCTYPE html> in new projects.
  • window.scrollY often matches scrollingElement.scrollTop for the main document.
  • Related: documentElement, body, compatMode.

Browser Support

Document.scrollingElement is marked Baseline Widely available on MDN (since August 2016). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.scrollingElement

Read-only Element that scrolls the document — scrollTop, scroll to top, standards vs quirks.

Widely Available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer IE11+ supported
Full support
Document.scrollingElement Baseline support

Bottom line: Use document.scrollingElement for reliable page scroll logic. In standards mode it matches documentElement; pair with window.scrollTo when you prefer the Window API.

Conclusion

Document.scrollingElement tells you which Element scrolls the page. In standards mode it is the root <html> element. Use scrollTop and scrollLeft for position, or set scrollTop = 0 to jump to the top as MDN demonstrates.

Continue with styleSheets, documentElement, body, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.scrollingElement for page scroll logic
  • Guard with if (document.scrollingElement) in edge cases
  • Use <!DOCTYPE html> so standards mode applies
  • Pair with window.scrollTo when that API is simpler
  • Read scrollTop for scroll-spy and progress UI

❌ Don’t

  • Hard-code only document.body for modern pages
  • Assume scrollingElement is never null
  • Confuse page scroll with overflow scroll inside a div
  • Assign to document.scrollingElement (read-only)
  • Rely on quirks mode behavior in new projects

Key Takeaways

Knowledge Unlocked

Five things to remember about document.scrollingElement

The standard Element that scrolls the document.

5
Core concepts
🌐02

Standard

documentElement

MDN
🔝03

Scroll top

scrollTop=0

MDN
⚠️04

Quirks

body|null

Edge
05

Status

Baseline

2016+

❓ Frequently Asked Questions

A read-only reference to the Element that scrolls the document. MDN: in standards mode this is usually document.documentElement (the root <html> element).
No. MDN marks Document.scrollingElement as Baseline Widely available (since August 2016). It is the standard way to find the scrolling root.
MDN example: const scrollElm = document.scrollingElement; scrollElm.scrollTop = 0; You can also use window.scrollTo(0, 0).
In standards mode, yes — MDN says scrollingElement is the root element (documentElement). In quirks mode the behavior differs and may involve body or null.
MDN: in quirks mode scrollingElement returns the HTML body element if it exists and is not potentially scrollable; otherwise null.
Both work for page scroll. scrollingElement gives you the Element with scrollTop/scrollLeft. window.scrollTo is a convenient Window API for the same job.
Did you know?

MDN notes that in quirks mode, scrollingElement may return document.body or even null. That is why modern pages use <!DOCTYPE html> and standards mode, where scrollingElement === document.documentElement.

Next: styleSheets

Learn how to list and inspect CSS stylesheets linked or embedded in the document.

styleSheets →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful