Style topProperty
Style Object
Definition and Usage
The top property sets or returns the top position of a positioned element.
This property specifies the top position of the element, including: padding, scrollbar, border, and margin.
Tip:A positioned element is an element whose position property is set to: relative, absolute, or fixed.
Syntax
Set the top property:
Object.style.top="auto|length|%|inherit"
Return the top property:
Object.style.top
| Value | Description |
|---|---|
| auto | Default. The browser calculates the top position. |
| length | Defines the position of the element's top edge to the top edge of the nearest positioned parent element, using units such as px, cm. Negative values are allowed. |
| % | Defines the position of the element's top edge to the top edge of the nearest positioned parent element as a percentage. |
| inherit | The value of the top property is inherited from the parent element. |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the top property.
Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".
Example
Example
Set the top position of a button:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (example.com)</title>
<style type="text/css">
#b1{
position:absolute;
}
</style>
<script>
function displayResult(){
document.getElementById("b1").style.top="100px";
}
</script>
</head>
<body>
<input type="button" id="b1" onclick="displayResult()" value="Set top position to 100 px">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (example.com)</title>
<style type="text/css">
#b1{
position:absolute;
}
</style>
<script>
function displayResult(){
document.getElementById("b1").style.top="100px";
}
</script>
</head>
<body>
<input type="button" id="b1" onclick="displayResult()" value="Set top position to 100 px">
</body>
</html>
Try it Yourself »
Style Object Other Extensions