CSS margin-top - Changing the top Margin of an HTML Element
Description
This command is used to change the size of the top margin of an html element.
default value is 0.
margin-top: length;
or
margin-top: percentage;
or
margin-top: auto;
or
margin-top: inherit;
See also: margin, margin-left, margin-right, margin-bottom
Examples for margin-top
Example 1:
Simple example using a div (block level element) to show large 50px top margin. We add a border to make it easy to see the margin. Note that
the margin is the distance between the border and the outer edge of the element (see the box model). So the border is not affected by the margin.
test text test text test text test text
<style>
#test { margin-top: 50px; border:1px solid black; }
#outer { border:1px solid black; }
</style>
<div id="outer">
<div id="test">test text test text test text test text</div>
</div>
Example 2:
Same example but this time with a span (an inline element). Two things to note, 1) span is an inline element therefore the margin overlaps the surrounding elements (in internet explorer anyway! In firefox the margin-top is ignored for inline elements)
- hence the border is visible over this text. And 2) inline elements do not collapse to the width of the enclosing element, the default size is to closely wrap the containing text and elements.
test text test text test text test text
<style>
#test2 { margin-top: 50px; border:1px solid black; }
#outer { border:1px solid black; }
</style>
<span id="outer">
<span id="test2">test text test text test text test text</span>
</span>
Values for margin-top
| margin-top |
length % auto
|
Specifies the size of the top margin. |