CSS Property margin-top








margin-top sets the width of the top margin for an element. Negative values are permitted.

Summary

ItemValue
Initial value 0
Inherited No.
Version CSS1
JavaScript syntax object.style.marginTop="10px"
Applies to All elements.




CSS Syntax

margin-top: length | percentage | auto | inherit 

Property Values

The property values are listed in the following table.

ValueDescription
auto Default value. The browser does the calculation.
length Set width in px, cm, etc.
% Set width in percent of the containing element
inherit Inherit the width property from the parent element

Browser compatibility

margin-top Yes Yes Yes Yes Yes




Example

The following code uses the inch and em to set the top margin.

ul {margin-top: 0.5in;} 
h3 {margin-top: 1.5em;}
<!DOCTYPE HTML>
<html>
    <head>
        <style rel='stylesheet' type='text/css'>
<!--from   www. ja  v  a 2s  .c o m-->
div {
    width: 100px;
    height: 100px;
    background: mistyrose;
    border: 1px solid pink;
}
div#topbottom-rightleft {
    margin-top: 15px;
    margin-right: 5px;
    margin-bottom: 15px;
    margin-left: 5px;
}
      
        </style>
    </head>
    <body>
        <div id='topbottom-rightleft'></div>
        
    </body>
</html>

Click to view the demo