I have a div that contains a link:
<div id="like_bar"><a href="#" onclick="return false;" id="like"></a></div>
With some CSS:
#like_bar{
width:140px;
height:26px;
background:url('bar.jpg');
}
#like{
display:block;
width:20px;
height:20px;
margin:3px 36px;
background:url('mini_img.png');
}
The link is placed at the top of the bar and the margins on the link are applied to the bar.
This is annoying.
Could someone explain these collapsing margins, how to avoid them and what they’re used for.
There are many ways to “fix this”.
Perhaps the easiest for you would be this:
Other ways include:
paddingborder(evenborder: 1px solid transparentis enough)floatthe elementposition: absoluteoverflowto a value other than the default ofvisible.You also asked:
A common use case is the
<p>tag.See: http://jsfiddle.net/thirtydot/tPaTY/
Without margin collapsing, certain things would become annoying.