top Property - Javascript CSS Style Property

Javascript examples for CSS Style Property:top

Description

The top property sets or gets the top position of a positioned element.

Property Values

Value Description
autoLets the browser calculate the top position. Default
length Sets the top position in length units. Negative values are allowed
% Sets the top position in percentage?of the height of the parent element
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

Technical Details

Item Value
Default Value: auto
Return Value: A String, representing the top position of a positioned element
CSS VersionCSS2

Set the top position of a <button> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<div id="myDiv" style="position:absolute;top:75px;">This is a div.</div>

<button type="button" onclick="myFunction()">Return top position of div</button>

<script>
function myFunction() {//from   w  w w  .  ja  va 2 s  .  c om
    document.getElementById("myDiv").style.top = '20px';
}
</script>

</body>
</html>

Related Tutorials