Example 1 — Read scrollTop
Before scrolling, the value is usually 0.
const box = document.getElementById("notes");
console.log(box.scrollTop); How It Works
At the start of a vertically scrollable box, scrollTop is typically zero.

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.
Instance property
Read and set
Number (pixels)
Vertical scroll
Baseline widely
scrollHeight, clientHeight
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.
const box = document.getElementById("notes");
console.log(box.scrollTop); // 0 at the start
box.scrollTop += 20; scrollTop is the vertical counterpart of scrollLeft. Both are readable and writable.
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.
element.scrollTop
element.scrollTop = 100
element.scrollTop += 20 A double-precision floating-point number representing vertical scroll offset in CSS pixels.
| Item | Detail |
|---|---|
| Type | Number (pixels) |
| Access | Readable and writable |
| Default | 0 when not scrolled |
| Precision | Can include decimal subpixels |
| Related | scrollHeight, clientHeight, scroll() |
Because scrollTop can contain decimals while scrollHeight / clientHeight are rounded, use a small threshold when detecting scroll-to-bottom.
Increase scrollTop to move down, or combine it with height properties to detect the bottom:
box.scrollTop += 20;
function isAtBottom(element) {
return (
Math.abs(element.scrollHeight - element.clientHeight - element.scrollTop) <= 1
);
} Related learning: scrollHeight, clientHeight, and scrollLeft.
| Goal | Code / note |
|---|---|
| Read vertical scroll | el.scrollTop |
| Scroll down by 20px | el.scrollTop += 20 |
| Jump to position | el.scrollTop = 100 |
| Reset to top | el.scrollTop = 0 |
| At bottom? | Math.abs(el.scrollHeight - el.clientHeight - el.scrollTop) <= 1 |
| MDN status | Baseline Widely available |
Four facts about Element.scrollTop.
get/setInstance
numberPixels
verticalUp/down
widelyJul 2015+
Examples follow MDN Element: scrollTop. Labs use vertically scrollable boxes so you can read and change the scroll position.
Start by reading the current vertical scroll position.
scrollTopBefore scrolling, the value is usually 0.
const box = document.getElementById("notes");
console.log(box.scrollTop); At the start of a vertically scrollable box, scrollTop is typically zero.
Slide down, jump to a position, and detect scroll-to-bottom.
Increase scrollTop by 20 pixels when a button is clicked.
button.onclick = () => {
document.getElementById("notes").scrollTop += 20;
}; Adding to scrollTop moves the visible area down and reveals more content below.
Assign a number to jump directly to that vertical scroll offset.
const box = document.getElementById("notes");
box.scrollTop = 80;
console.log(box.scrollTop); Setting scrollTop scrolls immediately, similar to element.scroll() with behavior: "auto".
Use a threshold pattern with scrollHeight and clientHeight.
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)); The threshold avoids rounding issues between decimal scrollTop and rounded height values.
Feature-detect and remember the read/write behavior.
console.log({
supported: "scrollTop" in Element.prototype,
access: "read and write",
precision: "subpixel possible",
status: "Baseline Widely available (MDN)"
}); Safe to use widely. It is one of the core properties for controlling vertical scrolling in JavaScript.
Overflow creates a vertical scrollable area.
scrollTop stores how far content moved from the top edge.
Reading inspects position; writing scrolls the element.
That is how buttons, chat UIs, and custom scroll controls move vertically.
clientHeight, scroll(), JavaScript hub.Element.scrollTop is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
Readable and writable — returns or sets vertical scroll offset in pixels.
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.
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.
scrollTop += value for step-by-step vertical movementscrollTop = 0 for “back to top” actionsscrollHeight and clientHeight for bounds logicscrollTop with scrollHeightscrollTopReadable and writable vertical scroll offset in pixels.
on Element
Kindpixels
Typescroll offset
Usewidely available
Statusfor bottom checks
TipSetting element.scrollTop scrolls immediately, the same way as calling element.scroll() with behavior: "auto".
Learn the non-standard Firefox maximum horizontal scroll helper.
5 people found this page helpful