
- 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 scrollTop Property
The HTML DOM Element scrollTop property allows you to get (retrieve) and update (set) how far an element's content is scrolled vertically.
It returns the distance in pixels from the top edge of the element. The Pixels (px) is a unit used in CSS to manage property values.
Syntax
Following is the syntax of the HTML DOM element scrollTop property (to set the property) −
element.scrollTop = pixelValue;
Here, the pixelValue is a new value that sets the vertical scroll position of the element.
Use the following syntax to get the scrollTop property value −
element.scrollTop;
Parameters
Since this is a property, it will not accept any parameter.
Return Value
The scrollTop property returns an integer value that holds the vertically scrolled position in pixels.
Example 1: Displaying Real-time Scroll Position
The following program demonstrates using the HTML DOM Element scrollTop property. It will display the real-time scrollTop property value −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element scrollTop</title> <style> #myDIV { height: 100px; width: 200px; overflow: auto; border: 1px solid black; padding: 10px; } </style> </head> <body> <h3>HTML DOM Element scrollTop Property</h3> <div id="myDIV"> <p>Scroll Me!!</p> <p>To get the scrolled position.</p> <p>Some content inside the scrollable container.</p> </div> <p>Scroll position: <span id="scrollPos">0px</span></p> <script> const m = document.getElementById('myDIV'); m.addEventListener('scroll', function() { const sp = m.scrollTop; document.getElementById('scrollPos').innerHTML = sp + 'px'; }); </script> </body> </html>
The above program displays the real-time scroll position value.
Example 2: Setting scrollTop Property Value
Here is another example of using the HTML DOM Element scrollTop property. This method allows us to set the scrollTop property for a div element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element scrollTop</title> <style> .scr { height: 100px; width: 200px; overflow-y: scroll; border: 1px solid #ccc; padding: 10px; transition: scroll 0.5s; scroll-behavior: smooth; } </style> </head> <body> <h3>HTML DOM Element scrollTop Property</h3> <div class="scr" id="scrDiv"> <p>This example allows <br>you to set the <br> scroll position to 50 pixel, <br> which upon clicking <br>the button will adjust the scrollable position to <br>50 pixel. </p> </div><br> <button onclick="setScrollPosition()">Set Scroll Position to 50px</button> <script> function setScrollPosition() { var ele=document.getElementById('scrDiv'); ele.scrollTop = 50; } </script> </body> </html>
Once the above program is executed, it will set the scrollTop property to the "div" element when the button is clicked.
Example 3: Changing Background Color on Scroll
This example demonstrates the use of the scrollTop property. When you scroll, the background color will change to green once the scroll exceeds 100 pixels from the top of the page −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element scrollTop</title> <style> body { margin: 0; height: 2000px; } header { background-color: #333; color: #fff; text-align: center; padding: 10px 0; } .content { margin-top: 100px; padding: 20px; text-align: center; font-size: 18px; } .highlight { background-color: green; color: white; } </style> </head> <body> <header><h1>ScrollTop Event Example</h1></header> <div class="content" id="myContent"> <p>Scroll down to see the effect. Once you scroll past 100 pixels from the top, this paragraph will have a yellow background.</p> </div> <script> window.onscroll = function() { myFunction() }; function myFunction() { var scrollPosition = document.documentElement.scrollTop || document.body.scrollTop; if (scrollPosition > 100) { document.getElementById("myContent").classList.add("highlight"); } else { document.getElementById("myContent").classList.remove("highlight"); } } </script> </body> </html>
After executing the above program, the background color will be changed to green when the scrollTop property value > 100px.
Example 4: Clicking a Button Scrolls a Container Vertically
The example below uses the scrollTop property to adjust the scroll position by adding 30 pixels horizontally and 10 pixels vertically each time the button is clicked −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element scrollTop</title> <style> .con { height: 300px; width: 80%; padding: 10px; overflow: auto; border: 1px solid black; scroll-behavior: smooth; transition: scroll 0.5s; } .content { height: 800px; } </style> </head> <body> <h3>HTML DOM Element scrollTop Property</h3> <div class="con"> <div class="content"> <p>Scroll Me by Clicking the Button!!.</p> <p>Below!.</p> <p>This is some content.</p> </div> </div> <br> <br> <button onclick="scrollContainer()">Scroll Container</button> <script> function scrollContainer() { var con = document.querySelector('.con'); con.scrollLeft += 30; con.scrollTop += 10; } </script> </body> </html>
In the above program, the scrollTop position within the div element will increase 10px dynamically on each click.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
scrollTop | Yes | Yes | Yes | Yes | Yes |