JavaScript Element scrollIntoView() Method

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

What You’ll Learn

Element.scrollIntoView() is an instance method from the CSSOM View module. It scrolls ancestor containers until this element is visible. Learn block / inline alignment, behavior: "smooth", the Promise interrupted flag, and five try-it labs.

01

Kind

Instance method

02

Action

Reveal element

03

block

start / center / end

04

Returns

Promise

05

behavior

smooth / instant

06

Status

Baseline widely

Introduction

Long pages, tables of contents, and form validation often need to bring a specific element into view. Call element.scrollIntoView() and the browser scrolls every relevant ancestor until that element is visible.

MDN: you can pass a legacy boolean (true aligns top, false aligns bottom) or a modern options object with behavior, block, inline, and container. Modern browsers return a Promise with { interrupted }.

💡
Beginner tip

Use scrollIntoView() when you have a target element (anchor link, error field, last chat message). Use scroll() when you know exact pixel coordinates inside one scroll container.

This page is part of JavaScript Element. Related: scroll(), scrollBy(), and the JavaScript hub.

Understanding the scrollIntoView() Method

Calling el.scrollIntoView(), el.scrollIntoView(true), or el.scrollIntoView({ block, inline, behavior, container }) scrolls scrollable ancestors so el appears in the viewport (MDN).

  • It is an instance method on Element (CSSOM View).
  • Scrolls ancestor containers (and optionally the viewport) until the element is visible.
  • Returns a Promise with { interrupted: boolean } (MDN).
  • block: start, center, end, nearest (default start).
  • inline: same values (default nearest).
  • Pair with CSS scroll-margin when sticky headers overlap targets (MDN).

📝 Syntax

General forms of Element.scrollIntoView (MDN):

JavaScript
scrollIntoView()
scrollIntoView(alignToTop)
scrollIntoView(options)

Parameters

  • alignToTop (boolean, optional) — true aligns top to visible area (default); false aligns bottom (MDN).
  • options (object):
    • behavior"smooth", "instant", or "auto" (default)
    • block — vertical: start, center, end, nearest (default start)
    • inline — horizontal: same values (default nearest)
    • container"all" (default) or "nearest"

Return value

A Promise that fulfills with { interrupted: boolean } (MDN).

Exceptions

  • No standard exceptions documented; non-scrollable contexts may simply not move.

Common patterns

JavaScript
const target = document.getElementById("section-3");

target.scrollIntoView();                              // MDN basic
target.scrollIntoView({ block: "center", behavior: "smooth" });

const result = await target.scrollIntoView({ block: "start" });
console.log(result.interrupted);

⚡ Quick Reference

GoalCode
Reveal elementel.scrollIntoView()
Center in viewel.scrollIntoView({ block: "center" })
Smooth scrollel.scrollIntoView({ behavior: "smooth" })
Fixed header offsetscroll-margin-top on target
Return valuePromise<{ interrupted }>
MDN statusBaseline Widely available (since January 2020)

🔍 At a Glance

Four facts to remember about Element.scrollIntoView().

Returns
Promise

{ interrupted }

Baseline
widely

Since Jan 2020

block
start

Vertical align

Target
element

Not coords

📋 scrollIntoView() vs scroll() vs scrollBy()

scrollIntoView()scroll()scrollBy()
InputTarget elementAbsolute coordsRelative delta
ScrollsAncestors + viewportOne elementOne element
block / inlineYesNoNo
Promise returnYes (MDN)Yes (MDN)Yes (MDN)
Best forAnchor links, focusJump to pixelNudge by pixels

Examples Gallery

Examples follow MDN Element.scrollIntoView() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN basic reveal and smooth alignment options.

Example 1 — Basic scrollIntoView() (MDN)

Scroll ancestors until the element is visible (top aligned by default).

JavaScript
const element = document.getElementById("box");

element.scrollIntoView();
element.scrollIntoView(false);              // align bottom (MDN)
element.scrollIntoView({ block: "end" });
Try It Yourself

How It Works

MDN: with no arguments, the element’s top edge aligns to the top of the scrollable ancestor. Passing false aligns the bottom edge instead.

Example 2 — Smooth behavior (MDN)

Animate scrolling when revealing an element.

JavaScript
element.scrollIntoView({
  behavior: "smooth",
  block: "end",
  inline: "nearest",
});
Try It Yourself

How It Works

behavior: "smooth" animates the scroll. inline: "nearest" is MDN’s default for horizontal alignment.

📈 Practical Patterns

block alignment, fixed headers, and MDN end-paragraph demo.

Example 3 — Center With block: "center"

Scroll a section heading to the middle of the viewport.

JavaScript
document.querySelectorAll(".toc a").forEach((link) => {
  link.addEventListener("click", (e) => {
    e.preventDefault();
    const id = link.getAttribute("href").slice(1);
    document.getElementById(id).scrollIntoView({
      behavior: "smooth",
      block: "center",
    });
  });
});
Try It Yourself

How It Works

Table-of-contents links often use block: "center" so the heading lands in the middle of the screen instead of flush against the top.

Example 4 — scroll-margin-top With Fixed Header (MDN)

Leave space for a sticky navbar so the target isn’t hidden underneath.

JavaScript
#go-to-bottom { scroll-margin-top: 60px; }
JavaScript
goToBottom.addEventListener("click", () => {
  goToTop.scrollIntoView({ behavior: "instant", block: "start" });
});
Try It Yourself

How It Works

MDN: without scroll-margin-top, a fixed header can cover the element after scrollIntoView(). The margin offsets the scroll snap point.

Example 5 — Scroll Last Paragraph Into View (MDN Demo)

Reveal #end inside a scrollable <section> and await the Promise.

JavaScript
const end = document.querySelector("#end");

document.querySelector(".scroll-into-view").addEventListener("click", async () => {
  const result = await end.scrollIntoView();
  console.log("interrupted:", result.interrupted);
});
Try It Yourself

How It Works

MDN’s element methods demo scrolls the last paragraph into view inside an overflow section and uses the Promise to fade UI back in after smooth scrolling.

🚀 Common Use Cases

  • Table-of-contents links that jump to page sections.
  • Scrolling to the first invalid form field after validation fails.
  • Revealing the latest chat message or comment in a thread.
  • Animating to a card with behavior: "smooth" and block: "center".
  • Offsetting sticky headers with scroll-margin-top (MDN).
  • Syncing UI after scroll completes via the Promise interrupted flag.

🧠 How scrollIntoView() Reveals an Element

1

Pick the target element

Call scrollIntoView() on the element you want visible.

Target
2

Browser finds scrollable ancestors

Scrolls containers (and viewport) per container option (MDN).

Ancestors
3

Align with block / inline

Positions the element using block and inline alignment rules.

Align
4

Promise resolves with interrupted

Element is visible; run follow-up UI when smooth scroll finishes (MDN).

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available since January 2020).
  • scrollIntoView() scrolls ancestors until the element is visible (MDN).
  • Returns a Promise with { interrupted: boolean } (MDN).
  • Legacy boolean: true = top align, false = bottom align (MDN).
  • Use CSS scroll-margin to offset fixed/sticky headers (MDN).
  • Related: scroll(), scrollBy(), scrollTop, JavaScript hub.

Browser Support

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

Baseline Widely available

Element.scrollIntoView()

Safe for production. Scroll ancestors until an element is visible, with optional smooth behavior.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Legacy scrollTop only · no Promise return
No
scrollIntoView() Excellent

Bottom line: Use scrollIntoView() to reveal a target element, scroll-margin for fixed headers, and await the Promise when smooth scrolling must finish before UI updates.

Conclusion

Element.scrollIntoView() scrolls ancestor containers until the element is visible. It supports block / inline alignment, smooth animation, and returns a Promise you can await for polished scroll UX.

Continue with scrollBy(), scroll(), scrollIntoViewIfNeeded(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use scrollIntoView() for anchor links and focus targets
  • Use block: "center" for TOC-style navigation
  • Add scroll-margin-top under fixed headers
  • Await the Promise to run UI after smooth scroll ends
  • Prefer the options object over the legacy boolean

❌ Don’t

  • Confuse scrollIntoView() with scroll()
  • Forget sticky navbars can cover the target
  • Assume IE returns a Promise
  • Fire many smooth scrolls without handling interrupted
  • Call on elements that are already fully visible unless you need alignment

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.scrollIntoView()

Reveal a target element—align with block/inline and optional smooth scroll.

5
Core concepts
📄 02

Reveal

ancestors

Visible
✍️ 03

block

start/center

Align
04

Baseline

widely available

Status
05

Pair

scroll-margin

Headers

❓ Frequently Asked Questions

It scrolls the element's ancestor containers so the element becomes visible to the user (MDN). You can pass a boolean or an options object.
No. MDN marks Element.scrollIntoView() as Baseline Widely available (since January 2020). It is not Deprecated, Experimental, or Non-standard.
A Promise that fulfills with { interrupted } — true if another programmatic scroll interrupted this one before it finished (MDN).
scrollIntoView() scrolls ancestors until this element is visible — you don't pick pixel coordinates. scroll() jumps to absolute offsets inside one scrollable element.
block controls vertical alignment (start, center, end, nearest). inline controls horizontal alignment (start, center, end, nearest). Defaults are start and nearest (MDN).
Use CSS scroll-margin-top or scroll-margin-bottom on the target element so scrollIntoView() leaves space for sticky/fixed navbars (MDN).
Did you know?

scrollIntoView() is what powers many in-page anchor links under the hood. Pair it with CSS scroll-margin when a sticky header would otherwise cover your target.

Next: scrollIntoViewIfNeeded()

Continue with the non-standard WebKit scroll-if-hidden method.

scrollIntoViewIfNeeded() →

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.

8 people found this page helpful