UI APIs: How to get the scroll position of an element in JavaScript

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


How can you do that?

Once you have the element, you can inspect its scrollLeft and scrollTop properties.

The 0, 0 position is always found in the top left corner, so any scrolling is relative to that.

Example:

const container = document.querySelector('. container')
container.scrollTop
container.scrollLeft

Those properties are read/write, so you can also set the scroll position:

const container = document.querySelector('. container')
container.scrollTop = 1000
container.scrollLeft = 1000

Lessons in this unit:

0: Introduction
1: Clipboard API
2: Scrolling
3: Console API
4: Speech Synthesis API
5: ▶︎ How to get the scroll position of an element in JavaScript
6: How to make a page editable in the browser
7: Safari, warn before quitting