Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Document and Elements

Element.scrollIntoView()

Reference for Element.scrollIntoView(): scroll the nearest scrollable ancestors so an element becomes visible, with smooth scrolling, block and inline options.

The scrollIntoView() method scrolls every scrollable ancestor of an element, including the page itself, until the element is visible. You can ask for smooth animation and choose where the element should land: at the top, the center or the bottom of the scrolling area.

It is the quickest way to jump to a search result, reveal the first invalid field of a form or show a newly added chat message. Nothing is returned and nothing in the DOM changes; only scroll positions do.

JavaScript
element.scrollIntoView();
element.scrollIntoView({ behavior: "smooth", block: "center" });

Try it

Jump to a section
Scroll the page to the first error

Syntax

JavaScript
element.scrollIntoView()
element.scrollIntoView(alignToTop)
element.scrollIntoView(options)

Parameters

Name Description
alignToTop Optional boolean. true (the default) behaves like { block: "start", inline: "nearest" }; false behaves like { block: "end", inline: "nearest" }.
options.behavior "auto" (default; follows the CSS scroll-behavior of the scroller), "smooth" or "instant".
options.block Vertical alignment: "start" (default), "center", "end" or "nearest".
options.inline Horizontal alignment: "start", "center", "end" or "nearest" (default).

Return value

undefined.

Usage notes

  • nearest is the gentlest choice. With block: "nearest" nothing moves if the element is already fully visible, and otherwise the smallest possible scroll is used. This is ideal for keyboard navigation in lists and menus.
  • Several containers can scroll at once. The browser scrolls the element's nearest scrollable ancestor first, then that ancestor's ancestors, so the page itself may move too. If you only want an inner box to scroll, set its scrollTop yourself.
  • Fixed headers cover the result. A sticky or fixed header can hide the top of the element. Give the target a CSS scroll-margin-top that matches the header height.
  • Smooth scrolling is a request. Users who prefer reduced motion may have it turned off, and behavior: "auto" obeys the CSS scroll-behavior property. Do not rely on a scroll having finished right after the call; use the scrollend event where available.
  • Hidden elements cannot scroll. If the element has display: none, there is nothing to scroll to and the call has no effect.
  • Focus is separate. Scrolling does not move keyboard focus. Pair it with focus() when you reveal an input, and pass preventScroll: true to avoid a second jump.
  • Links with #id do the same job without JavaScript, and they also work for people with scripting disabled, so prefer them for plain page navigation.

Browser support

The basic call and the boolean argument work in every browser. The options object, including behavior: "smooth", is supported in all current browsers; "instant" and the scrollend event are newer, so check a compatibility table.