The scrollTo() method jumps the page to absolute scroll coordinates. This tutorial covers syntax, smooth scrolling, back-to-top buttons, scrolling to elements, how it differs from scrollBy(), five examples, and when to prefer scrollIntoView().
01
Syntax
scrollTo(x, y)
02
Absolute
Exact coordinates
03
vs scrollBy
Relative offset
04
Smooth
behavior: "smooth"
05
Top
scrollTo(0, 0)
06
Elements
offsetTop / intoView
Fundamentals
Introduction
Users expect “Back to top” links, table-of-contents jumps, and smooth navigation to page sections. The window.scrollTo() method moves the viewport to specific pixel coordinates in the document—unlike scrollBy(), which moves relative to where you already are.
scrollTo() works on normal browser tabs and is supported everywhere. Pair it with behavior: "smooth" for animated scrolling, or use CSS scroll-behavior: smooth on html for hash links without JavaScript.
Concept
Understanding the scrollTo() Method
window.scrollTo(x, y) sets the horizontal and vertical scroll position of the document. After the call, window.scrollX should equal x and window.scrollY should equal y (within document bounds). The method returns undefined.
Modern browsers accept ScrollToOptions: scrollTo({ top: 500, left: 0, behavior: "smooth" }). Use top / left in the object form and x / y in the positional form—they map to the same coordinates.
💡
Beginner Tip
scrollTo(0, 0) is the classic back-to-top. To scroll a heading into view with less math, element.scrollIntoView({ behavior: "smooth" }) is often easier.
Viewport scrolls so #pricing aligns with the top of the page.
How It Works
offsetTop is relative to the offset parent. For complex layouts, getBoundingClientRect().top + window.scrollY or scrollIntoView() may be more reliable.
Example 4 — Horizontal Scroll to a Position
Jump sideways in a wide document or scrollable container.
Click FAQ → page smoothly scrolls to the #faq section.
How It Works
For simple anchor links, <a href="#faq"> with CSS smooth scroll may be enough. JavaScript gives you full control over timing and offset (e.g. fixed header height).
Applications
🚀 Common Use Cases
Back to top — scrollTo({ top: 0, behavior: "smooth" }).
Table of contents — jump to section headings on long docs.
Onboarding tours — scroll to highlighted UI regions.
Form validation — scroll to the first invalid field.
Single-page apps — reset scroll on route change with scrollTo(0, 0).
Horizontal timelines — scrollTo({ left: x }) on wide content.
🧠 What Happens When You Call scrollTo()
1
Pass coordinates
Script supplies target x and y (or ScrollToOptions).
Args
2
Clamp to bounds
Browser limits values to valid scroll range (0 to max scroll).
Limits
3
Animate (optional)
With behavior: "smooth", scroll animates over a short duration.
Smooth
4
📍
Viewport at target
scrollX / scrollY match target; method returns undefined.
Important
📝 Notes
Fixed headers may cover content scrolled to with offsetTop—subtract header height or use scroll-margin-top in CSS.
Checking if ("scrollTo" in window) is unnecessary in modern browsers—the method is universal.
Respect prefers-reduced-motion: reduce—use instant scroll when users request reduced motion.
scrollTo on window moves the document; on an element it sets that container’s scrollTop / scrollLeft.
SPAs often call scrollTo(0, 0) on navigation so new pages start at the top.
For hash links (#section), native behavior plus CSS may replace JavaScript entirely.
Compatibility
Browser Support
Window.scrollTo() is supported in every major browser. The ScrollToOptions form with behavior: "smooth" is supported in all modern browsers.
✓ Universal · Standard API
Window.scrollTo()
Supported in Chrome, Firefox, Safari, Edge, Opera, and mobile browsers. Positional and object forms work on window and scrollable elements.
100%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
scrollTo()Universal
Bottom line: Safe to use for back-to-top, section jumps, and SPA scroll restoration. Pair with scrollBy() and scrollIntoView() for complete control.
Wrap Up
Conclusion
window.scrollTo() jumps the viewport to absolute document coordinates. It is ideal for back-to-top buttons, section navigation, and resetting scroll position after route changes.
Use ScrollToOptions for smooth animation, prefer scrollIntoView() when targeting elements is enough, and reach for scrollBy() when you need relative steps from the current position.
Use scrollTo({ top: 0, behavior: "smooth" }) for back to top
Account for fixed header height when scrolling to sections
Reset scroll on SPA route changes with scrollTo(0, 0)
Respect prefers-reduced-motion
Consider scrollIntoView() for simpler element targeting
❌ Don’t
Confuse scrollTo coordinates with scrollBy deltas
Force smooth scroll when users prefer reduced motion
Assume offsetTop works in every nested layout
Scroll aggressively without user intent
Feature-detect scrollTo in modern browsers unnecessarily
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about scrollTo()
Your foundation for absolute page scrolling in JavaScript.
5
Core concepts
📝01
Syntax
scrollTo(x, y)
API
📍02
Absolute
Exact coords.
Jump
🎨03
Smooth
behavior option.
UX
↑04
Top
(0, 0)
Pattern
🔄05
vs scrollBy
Relative step.
Compare
❓ Frequently Asked Questions
It scrolls the document to absolute coordinates. scrollTo(0, 500) jumps so the top of the viewport is 500 pixels from the top of the document (unless clamped at the bottom).
scrollTo(x, y) sets the scroll position directly. scrollBy(dx, dy) adds to the current position. Use scrollTo for “go to top” or a known coordinate; use scrollBy for “move down one screen from here.”
Pass a ScrollToOptions object: scrollTo({ top: 0, behavior: "smooth" }). The default behavior is "auto" (instant).
No. It returns undefined. Read window.scrollX and window.scrollY after scrolling to verify the new position.
scrollIntoView() is simpler for most cases—it handles nested scroll containers and alignment. scrollTo({ top: el.offsetTop }) works when you need exact pixel control relative to the document.
Yes for the main document—it scrolls to the top-left origin. On a scrollable element, element.scrollTo(0, 0) resets that element’s scroll position.
Did you know?
Adding scroll-behavior: smooth to your html element makes native <a href="#section"> links animate smoothly—often replacing custom scrollTo() code for simple in-page navigation.