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.
element.scrollIntoView(); element.scrollIntoView({ behavior: "smooth", block: "center" });
Try it
Syntax
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
nearestis the gentlest choice. Withblock: "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
scrollTopyourself. - Fixed headers cover the result. A sticky or fixed header can hide the top of the element. Give the target a CSS
scroll-margin-topthat matches the header height. - Smooth scrolling is a request. Users who prefer reduced motion may have it turned off, and
behavior: "auto"obeys the CSSscroll-behaviorproperty. Do not rely on a scroll having finished right after the call; use thescrollendevent 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 passpreventScroll: trueto avoid a second jump. - Links with
#iddo 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.