HTML | DOM Style top Property
The Style top Property in HTML DOM is used to set or return the top position of a positioned element including padding, scrollbar, border and margin.
Syntax:
- It is used to return the top property:
object.style.top
- It is used to set the top property:
object.style.top = "auto|length|%|initial|inherit"
Property Value Description:
- auto- This value automatically sets the default top value by the browser.
- length- This value sets the top value in the specified length units. This specified length can be positive as well as negative.
- %- Percentage value sets the top value in the specified percentage of the parent element’s width.
- initial- This value sets the top property to its browser’s default value.
- inherit- This value sets the top property to the value from its parent element.
Return Value: A String, representing the top position of a positioned element.
Example 1:
<!DOCTYPE html> <html> <head> <title> HTML | DOM Style top Property </title> <style> #myBtn { position: absolute; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2> HTML DOM Style top Property </h2> <button type="button" id="myBtn" onclick="myFunction()">Click here! </button> <script> function myFunction() { document.getElementById("myBtn") .style.top = "200px"; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before Click on the button:
- After Click on the button (Note that click here button moved down):
Example 2:
<!DOCTYPE html><html><head><title>HTML | DOM Style top Property</title><style>#GfG {border: 1px solid #FF0000;position: relative;}</style></head><body><h1> HTML|DOM Style top Property</h2><divid="GfG">Welcome to Geeks for Geeks.</div><br><buttontype="button"onclick="myFunction()">Click Here!</button><script>function myFunction() {document.getElementById("GfG").style.top = "-20px";}</script></body></html>chevron_rightfilter_noneOutput:
- Before click on the button:
- After click on the button:
Supported Browsers: The browser supported by DOM style top property are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | DOM Style right Property
- HTML | DOM Style flexFlow Property
- HTML | DOM Style perspectiveOrigin Property
- HTML | DOM Style orphans Property
- HTML | DOM Style borderCollapse Property
- HTML | DOM Style flex Property
- HTML | DOM Style tableLayout Property
- HTML | DOM Style emptyCells Property
- HTML | DOM Style border Property
- HTML | DOM Style minHeight Property
- HTML | DOM Style quotes Property
- HTML | DOM Style animationName Property
- HTML | DOM Style marginLeft Property
- HTML | DOM Style justifyContent Property
- HTML | DOM Style counterReset Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
- Before click on the button:



