JavaScript Element scrollTop Property

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

What You’ll Learn

Element.scrollTop is an instance property that gets or sets how far an element’s content is scrolled vertically. Learn how to read the current position, move content down, assign a specific value, and detect the bottom with scrollHeight—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read and set

03

Type

Number (pixels)

04

Direction

Vertical scroll

05

Status

Baseline widely

06

Pairs with

scrollHeight, clientHeight

Introduction

When a box has more content than fits vertically, the user can scroll up and down. The scrollTop property tells you how many pixels the content has moved from the top edge, and you can also change that value from JavaScript.

It is useful for chat windows, infinite lists, sticky headers, “scroll to top” buttons, and any UI where you need to move content vertically programmatically.

JavaScript
const box = document.getElementById("notes");
console.log(box.scrollTop); // 0 at the start
box.scrollTop += 20;
💡
Beginner tip

scrollTop is the vertical counterpart of scrollLeft. Both are readable and writable.

Understanding the Property

MDN: the scrollTop property gets or sets the number of pixels by which an element’s content is scrolled from its top edge. This value is subpixel precise in modern browsers.

  • Readable and writable — inspect or control vertical scroll position.
  • Starts at 0 — when the element has not been scrolled vertically.
  • Positive means scrolled down — revealing more content below.
  • Can be decimal — subpixel precision is possible on modern devices.

📝 Syntax

JavaScript
element.scrollTop
element.scrollTop = 100
element.scrollTop += 20

Value

A double-precision floating-point number representing vertical scroll offset in CSS pixels.

ItemDetail
TypeNumber (pixels)
AccessReadable and writable
Default0 when not scrolled
PrecisionCan include decimal subpixels
RelatedscrollHeight, clientHeight, scroll()
⚠️
Bottom checks need a threshold

Because scrollTop can contain decimals while scrollHeight / clientHeight are rounded, use a small threshold when detecting scroll-to-bottom.

📋 MDN Scroll-Down & Bottom Patterns

Increase scrollTop to move down, or combine it with height properties to detect the bottom:

JavaScript
box.scrollTop += 20;

function isAtBottom(element) {
  return (
    Math.abs(element.scrollHeight - element.clientHeight - element.scrollTop) <= 1
  );
}

Related learning: scrollHeight, clientHeight, and scrollLeft.

⚡ Quick Reference

GoalCode / note
Read vertical scrollel.scrollTop
Scroll down by 20pxel.scrollTop += 20
Jump to positionel.scrollTop = 100
Reset to topel.scrollTop = 0
At bottom?Math.abs(el.scrollHeight - el.clientHeight - el.scrollTop) <= 1
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.scrollTop.

Kind
get/set

Instance

Type
number

Pixels

Moves
vertical

Up/down

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: scrollTop. Labs use vertically scrollable boxes so you can read and change the scroll position.

📚 Getting Started

Start by reading the current vertical scroll position.

Example 1 — Read scrollTop

Before scrolling, the value is usually 0.

JavaScript
const box = document.getElementById("notes");
console.log(box.scrollTop);
Try It Yourself

How It Works

At the start of a vertically scrollable box, scrollTop is typically zero.

📈 Move, Set & Bottom Detection

Slide down, jump to a position, and detect scroll-to-bottom.

Example 2 — Slide Down on Click

Increase scrollTop by 20 pixels when a button is clicked.

JavaScript
button.onclick = () => {
  document.getElementById("notes").scrollTop += 20;
};
Try It Yourself

How It Works

Adding to scrollTop moves the visible area down and reveals more content below.

Example 3 — Set a Specific Position

Assign a number to jump directly to that vertical scroll offset.

JavaScript
const box = document.getElementById("notes");
box.scrollTop = 80;
console.log(box.scrollTop);
Try It Yourself

How It Works

Setting scrollTop scrolls immediately, similar to element.scroll() with behavior: "auto".

Example 4 — Detect Scrolled to Bottom

Use a threshold pattern with scrollHeight and clientHeight.

JavaScript
function isAtBottom(element) {
  return (
    Math.abs(element.scrollHeight - element.clientHeight - element.scrollTop) <= 1
  );
}

const box = document.getElementById("notes");
box.scrollTop = box.scrollHeight;
console.log(isAtBottom(box));
Try It Yourself

How It Works

The threshold avoids rounding issues between decimal scrollTop and rounded height values.

Example 5 — Support Snapshot

Feature-detect and remember the read/write behavior.

JavaScript
console.log({
  supported: "scrollTop" in Element.prototype,
  access: "read and write",
  precision: "subpixel possible",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use widely. It is one of the core properties for controlling vertical scrolling in JavaScript.

🚀 Common Use Cases

  • Building “scroll to top” or “scroll to bottom” buttons.
  • Keeping a chat window pinned to the newest message.
  • Detecting when a user finished reading terms-and-conditions text.
  • Tracking vertical scroll progress for indicators or sticky headers.
  • Restoring a saved scroll position after navigation or reload.

🔧 How It Works

1

Element has taller content

Overflow creates a vertical scrollable area.

Layout
2

Browser tracks offset

scrollTop stores how far content moved from the top edge.

State
3

You read or write it

Reading inspects position; writing scrolls the element.

API
4

Content moves up or down

That is how buttons, chat UIs, and custom scroll controls move vertically.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Values can be decimal because of subpixel precision.
  • Safari overscroll and some layouts can produce negative values.
  • Related: scrollHeight, scrollLeft, clientHeight, scroll(), JavaScript hub.

Browser Support

Element.scrollTop is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.scrollTop

Readable and writable — returns or sets vertical scroll offset in pixels.

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
scrollTop Baseline

Bottom line: Use scrollTop to read or control vertical scrolling. Increase it to move down, set 0 to return to the top, and use a threshold with scrollHeight for bottom detection.

Conclusion

scrollTop is the vertical scroll position property. You can read it to inspect where a container is scrolled, or write to it to move content up and down programmatically.

Continue with scrollHeight, scrollLeft, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use scrollTop += value for step-by-step vertical movement
  • Reset with scrollTop = 0 for “back to top” actions
  • Use a threshold when detecting the bottom
  • Consider subpixel values when comparing positions
  • Pair with scrollHeight and clientHeight for bounds logic

❌ Don’t

  • Assume the value is always a whole number
  • Use exact equality for bottom detection
  • Confuse scrollTop with scrollHeight
  • Expect it to work on non-scrollable elements
  • Ignore user scroll position when building custom controls

Key Takeaways

Knowledge Unlocked

Five things to remember about scrollTop

Readable and writable vertical scroll offset in pixels.

5
Core concepts
📝02

number

pixels

Type
🔍03

Vertical

scroll offset

Use
04

Baseline

widely available

Status
🎯05

Use threshold

for bottom checks

Tip

❓ Frequently Asked Questions

It gets or sets how many pixels the element's content is scrolled vertically from its top edge. A positive value means the content has moved down.
No. MDN marks Element.scrollTop as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
It is usually 0 when the element has not been scrolled vertically. Some layouts or Safari overscroll can produce negative values.
Yes. Modern browsers can return subpixel-precise floating-point values, so scrollTop is not always a whole number.
Increase scrollTop, for example: element.scrollTop += 20. You can also assign a specific value: element.scrollTop = 100.
Use a threshold because scrollTop can contain decimals: Math.abs(element.scrollHeight - element.clientHeight - element.scrollTop) <= 1.
Did you know?

Setting element.scrollTop scrolls immediately, the same way as calling element.scroll() with behavior: "auto".

Previous: scrollLeftMax

Learn the non-standard Firefox maximum horizontal scroll helper.

← scrollLeftMax

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.

5 people found this page helpful