
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element scrollIntoView() Method
The HTML DOM Element scrollIntoView() method is used to ensure that a particular element on a webpage or within a scrollable container becomes visible by adjusting the scroll position.
This method accepts an options object or a boolean parameter that helps to adjust the scrolling behavior. Following are the object values that we can provide to this method:
- Behavior − It defines the scrolling behavior. It can be auto (default) or smooth.
- Block − It defines the vertical alignment of the scrolled element. It can be start, center, end, or nearest.
- Inline − It defines the horizontal.
Syntax
Following is the syntax of the HTML DOM Element scrollIntoView() method −
element.scrollIntoView(Options);
Parameters
This method accepts a single parameter as listed below −
Parameter | Description |
---|---|
Options | Object that can be used to customize the scrolling behavior including options for smooth scrolling , vertical and horizontal alignment. |
Return Value
This method does not return any value.
Example 1: Scroll to an Element with Custom Options
The following example demonstrates the usage of the HTML DOM Element scrollIntoView() method by ensuring that a specified element is scrolled into view −
<!DOCTYPE html> <html lang="en"> <head> <style> .con { height: 200px; overflow-y: scroll; } .item { height: 200px; margin-bottom: 20px; background-color: lightblue; } </style> </head> <body> <h3>HTML DOM Element scrollIntoView() Method</h2> <p>Scroll elements to view item3!!.</p> <div class="con"> <div class="item" id="item1">Item 1</div> <div class="item" id="item2">Item 2</div> <div class="item" id="item3">Item 3</div> <div class="item" id="item4">Item 4</div> </div> <button onclick="scrollToItem('item3')">Scroll to Item 3</button> <script> function scrollToItem(id) { const its = document.getElementById(id); its.scrollIntoView({ behavior: 'smooth', block: 'center' }); } </script> </body> </html>
The above program displays a container with three pages (elements). When the user clicks the button, it scrolls the specified page into view.
Example 2: Scroll to Bottom of Page
Here is another example of the scrollIntoView() method. This method is used to ensure that a specified element is scrolled into view within a webpage or a scrollable container −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element scrollIntoView</title> </head> <body> <h3>HTML DOM Element scrollIntoView() Method</h3> <p>Click the button below to scroll smoothly to the bottom of the page:</p> <button onclick="scrollToBottom()">Scroll to Bottom</button> <div style="height: 2000px;">Scroll up to see the button</div> <script> function scrollToBottom() { const be = document.body; be.scrollIntoView ({behavior:'smooth', block:'end'}); } </script> </body> </html>
When the button is clicked, the page will scroll down smoothly to the end.
Example 3: Scroll Horizontally within a Container
In the example below, we use the scrollIntoView() method to scroll elements into view within a webpage or scrollable container. This program enables horizontal scrolling within the container −
<!DOCTYPE html> <html lang="en"> <head> <style> .con { width: 300px; white-space: nowrap; overflow-x: scroll; } .item { display: inline-block; width: 250px; height: 150px; margin-right: 20px; background-color: #f0b0ff; } </style> </head> <body> <h3>HTML DOM Elements scrollIntoView() Method</h3> <p>Click Scroll elements to view item3!!.</p> <div class="con" id="con"> <div class="item" id="item1">Item 1</div> <div class="item" id="item2">Item 2</div> <div class="item" id="item3">Item 3</div> <div class="item" id="item4">Item 4</div> <div class="item" id="item5">Item 5</div> </div><br> <button onclick="scrollToItem('item3')">Scroll to Item 3</button> <script> function scrollToItem(itemId) { const its = document.getElementById(itemId); its.scrollIntoView({behavior:'smooth',inline:'end',block:'nearest' }); } </script> </body> </html>
The above program displays a horizontally scrollable container. When the user clicks the button, it scrolls horizontally to the specified element position.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
scrollIntoView() | Yes | Yes | Yes | Yes | Yes |