References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript scrollTo() method

Method JavaScript All modern browsers
In short

The scrollTo() method scrolls the window (or a scrollable element) to an exact position. window.scrollTo(0, 0) jumps to the top; window.scrollTo({ top: 0, behavior: "smooth" }) animates it. Use coordinates when you know the target position; use scrollIntoView() to scroll to a specific element.

Overview

scrollTo() scrolls to a specific position. On window it scrolls the page; on a scrollable element it scrolls that element. You can pass coordinates (scrollTo(x, y)) or an options object, scrollTo({ top, left, behavior }), which also lets you request smooth animated scrolling. The classic use is a "scroll to top" button: window.scrollTo({ top: 0, behavior: "smooth" }).

It's an absolute scroll: it goes to the given position regardless of where you are now. Its relative sibling is scrollBy(), which scrolls by an offset from the current position (scrollBy(0, 100) moves down 100px). And to scroll a particular element into view without computing its coordinates, scrollIntoView() is usually more convenient.

The current scroll position is available from window.scrollY and window.scrollX (or element.scrollTop/scrollLeft for elements), which you can read before scrolling or to decide whether to show a back-to-top button. As with scrollIntoView(), smooth scrolling can also be set globally in CSS with scroll-behavior: smooth.

Syntax

window.scrollTo(x, y)
window.scrollTo({ top, left, behavior })

window.scrollTo(0, 0);                              // jump to top
window.scrollTo({ top: 0, behavior: "smooth" });   // smooth
element.scrollTo({ top: 200 });                    // scroll an element
window.scrollBy(0, 100);                            // relative scroll

Parameters

The scrollTo() method accepts the following parameters.

Parameter Description
x / options Either an x and y coordinate, or an options object with top, left and behavior ("auto" / "instant" / "smooth").

Try it: scrollTo() and scrollBy()

Scroll Tester Ready

Switch between absolute scrollTo() and relative scrollBy(), test smooth, instant, and auto behavior, and inspect live scrollTop coordinates in the console.

Example

Live example
<div id="box" style="height:90px;overflow:auto;border:1px solid #ccc;font:15px system-ui">
  <div style="height:300px;padding:8px">Long content... scroll me with the buttons.</div>
</div>
<button onclick="document.getElementById('box').scrollTo({top:200,behavior:'smooth'})" style="font:14px system-ui;margin-top:8px">Scroll down</button>
<button onclick="document.getElementById('box').scrollTo({top:0,behavior:'smooth'})" style="font:14px system-ui;margin-top:8px">Back to top</button>

Common scroll recipes

Patterns for reduced-motion back-to-top, sticky header offsets, and inner scroll containers.

Accessible back-to-top

Honor prefers-reduced-motion with 'instant' rather than 'auto', because 'instant' overrides any html { scroll-behavior: smooth } rule.

const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
window.scrollTo({
  top: 0,
  left: 0,
  behavior: reduced ? 'instant' : 'smooth'
});

scrollTo() vs scrollBy() vs scrollIntoView()

Choose a scroll API based on whether you have exact coordinates, a relative offset, or a target element.

Method Coordinates Target Scroll margin Best fit
scrollTo(options) Absolute from (0, 0) window or element No (manual offset) Back-to-top and restoring scroll state
scrollBy(options) Relative offset window or element No Carousel next/prev and keyboard paging
el.scrollIntoView() Element box Any DOM element Yes (scroll-margin-top) Anchor jumps, TOC links, form errors

Best practices

  • Use scrollTo({ top: 0, behavior: "smooth" }) for a smooth scroll-to-top.
  • Use scrollBy() for relative scrolling and scrollTo() for absolute positions.
  • To scroll to an element, scrollIntoView() is usually simpler.
  • Read the current position from window.scrollY / element.scrollTop.

Browser compatibility

Baseline: Widely available
Across browsers since March 2022 W3C CSSOM View (opens in new tab)

Numeric scrollTo(x, y) is universal. Smooth scrolling via the options object (behavior: "smooth") shipped in Safari 15.4 (March 2022) and works across all modern browsers.

Feature Chrome Edge Firefox Safari
window.scrollTo(x, y) ChromeSupported since 1+ EdgeSupported since 12+ FirefoxSupported since 1+ SafariSupported since 1+
scrollTo({ top, left, behavior: "smooth" })Safari < 15.4 ignored behavior: "smooth" ChromeSupported since 61+ EdgeSupported since 79+ FirefoxSupported since 36+ SafariSupported since 15.4+
element.scrollTo(options)Safari 10.1+ ({ top, left }); smooth in 15.4+ ChromeSupported since 61+ EdgeSupported since 79+ FirefoxSupported since 36+ SafariSupported since 10.1+
behavior: "instant"Overrides CSS scroll-behavior: smooth ChromeSupported since 61+ EdgeSupported since 79+ FirefoxSupported since 36+ SafariSupported since 15.4+

Frequently asked questions

How do I scroll to the top of the page?
Use window.scrollTo({ top: 0, behavior: "smooth" }), or window.scrollTo(0, 0) to jump instantly.
What is the difference between scrollTo() and scrollBy()?
scrollTo() scrolls to an absolute position; scrollBy() scrolls by a relative offset from where you are now.
What is the difference between scrollTo() and scrollIntoView()?
scrollTo() takes coordinates; scrollIntoView() scrolls a specific element into view without needing its position.
How do I make scrollTo() smooth?
Use the options form with behavior: "smooth".
Why is window.scrollTo({ top: 0, behavior: "smooth" }) not scrolling?
Three common causes. First, omitting top and left makes scrollTo() a no-op. Second, your layout may scroll inside an inner container (such as main { overflow-y: auto; height: 100vh; }) rather than window, so call container.scrollTo() instead. Third, overflow: hidden on html or body (or overflow: clip on any scroll container) prevents scrolling.
How do I account for a fixed header when using scrollTo()?
Compute el.getBoundingClientRect().top + window.scrollY - headerHeight and pass that value as top. Alternatively, add scroll-margin-top: 72px in CSS and call el.scrollIntoView({ behavior: "smooth" }).

Related tools