|
Set an Element's Margin
By Stephen Bucaro
Margin is the space around the outside of an element that separates it from other webpage elements.
Set the size of the margin using the margin property.
SPAN 1SPAN 2
The default margin is 0. Elements are right next to each other.
SPAN 1SPAN 2
The first span has its margin set to 6 pixels.
<span style="margin: 6px;">Margin set to 6 pixels</span>
SPAN 1SPAN 2
Both spans have their margins set to 6 pixels.
SPAN 1
SPAN 2
Even though both margins are
set to 0 pixels, there is a space between the two spans. This occurs when you code the spans on separate lines,
a continuing Internet Explorer layout bug.
Use the margin-top property to set just the top margin:
SPAN 1
Top margin set to 8 pixels
<span style="border-style: solid; border-width: 1px; background-color: #ffff00;">SPAN 1</span><br />
<span style="border-style: solid; border-width: 1px; margin-top: 8px; background-color: #ffff00;">Top margin set to 8 pixels</span>
Note that top and bottom margins
are "collapsed". This means instead of adding the bottom margin of an element to the top margin of the element
immediately below it, only the larger of the two values is used. As shown above, IE just ignores the margin-top property.
Use the margin-right property to set just the right margin:
Rignt margin set to 8 pixelsSPAN 2
<span style="border-style: solid; border-width: 1px; margin-right: 8px; background-color: #ffff00;">Rignt margin set to 8 pixels</span><span style="border-style: solid; border-width: 1px; margin: 0px; background-color: #ffff00;">SPAN 2</span>
Use the margin-bottom property to set just the bottom margin:
Set the Bottom Margin
Bottm margin set to 8 pixels
SPAN 1
<span style="border-style: solid; border-width: 1px; margin-bottom: 8px; background-color: #ffff00;">Bottm margin set to 8 pixels</span><br />
<span style="border-style: solid; border-width: 1px; background-color: #ffff00;">SPAN 1</span>
Note that top and bottom margins
are "collapsed". This means instead of adding the bottom margin of an element to the top margin of the element
immediately below it, only the larger of the two values is used. As shown above, IE just ignores the margin-bottom property.
Use the margin-left property to set just the bottom left:
SPAN 1Left margin set to 8 pixels
<span style="border-style: solid; border-width: 1px; margin: 0px; background-color: #ffff00;">SPAN 1</span>>span style="border-style: solid; border-width: 1px; margin-left: 8px; background-color: #ffff00;">Left margin set to 8 pixels</span>
|