marginTop Property - Javascript CSS Style Property

Javascript examples for CSS Style Property:marginTop

Description

The marginTop property sets or gets the top margin of an element.

Property Values

Value Description
% top margin in percentage of the width of the parent element
length top margin in length units
autoThe browser sets the top margin
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

Technical Details

Item Value
Default Value: 0
Return Value: A String, representing the top margin of an element
CSS VersionCSS1

Set the top margin of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {//ww w  .  j a  v a  2 s.c  om
    border: 1px solid #FF0000;
}
</style>
</head>
<body>

<div id="myDiv" style="margin-top:5cm;">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myDiv").style.marginTop = '10px';
}
</script>

</body>
</html>

Related Tutorials