HTML DOM Element scrollIntoView() Method

Definition and Usage

scrollIntoView() The method scrolls an element into the visible area of the browser window.

Example

Example 1

Scroll the element with id="content" into the visible area of the browser window:

const element = document.getElementById("content");
element.scrollIntoView();

Try it yourself

Example 2

Scroll to the top or bottom of an element:

const element = document.getElementById("content");
function scrollToTop() {
  element.scrollIntoView(true);
}
function scrollToBottom() {
  element.scrollIntoView(false);
}

Try it yourself

Syntax

element.scrollIntoView(align)

Parameter

Parameter Description
align

Optional. A boolean value indicating the alignment type:

  • true - The top of the element will align with the top of the visible area of the scrollable ancestor
  • false - The bottom of the element will align with the bottom of the visible area of the scrollable ancestor

If omitted, it will scroll to the top of the element.

Note:Due to the layout of other elements, some elements may not scroll all the way to the top or bottom.

Return value

None.

Browser support

All browsers support element.scrollIntoView():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support