Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do nested vertical margin collapses work?

Tags:

css

I am having a hard time grasping the concept of vertical margins collapsing in nested elements. I came an article at http://www.howtocreate.co.uk/tutorials/css/margincollapsing explaining how it works however am confused by its explanation. So in its example it cites that there are 2 elements as follows

<div style="margin-top:10px">
<div style="margin-top:20px">
A
</div>
</div>  

Seeing that the inner div has a margin of 20px, that is what will be applied for the entire block of code. What confuses me is everything after that and not yet looking about issues with Internet Explorer 7. Would someone be able to explain it for a complete newbie to CSS in a simplified manner?

like image 719
PeanutsMonkey Avatar asked Aug 24 '11 00:08

PeanutsMonkey


People also ask

How does margin collapse work?

Margin collapse occurs when vertically adjacent margins of block-level elements collide to share a general margin space. The size of this shared space is dictated by the larger number margin. You can visualize this as an arm wrestling match, where the larger margin will take over and win.

Do vertical margins add together?

They will not add up. Whichever is larger will be used.

Do left and right margins collapse?

Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins. This does not happen on left and right margins! Only top and bottom margins!

In which scenario margins will collapse?

If there is no border, padding, inline content, height , or min-height to separate a block's margin-top from its margin-bottom , then its top and bottom margins collapse. Some things to note: More complex margin collapsing (of more than two margins) occurs when the above cases are combined.


2 Answers

Two-ish rules to remember:

  1. If margins touch, they collapse.
  2. Nested items "snuggle" if only margin separates them.
  3. Elements outside the "Flow" behave differently. That is, this behavior does not apply the same to floated, or position:fixed, or position:absolute elements.

So for this HTML (nested divs) :

<div id="outer"> 
    <div id="inner">
        A
    </div>
</div> 

and this initial CSS:

#outer {
    margin-top:10px;
    background:blue;
    height: 100px;
}
#inner {
    margin-top:20px;
    background:red;
    height: 33%;   
    width: 33%;
}

The margin collapses to the max of the touching margins and the nested div "snuggles" to the start of the container, like so: (See it at jsFiddle.)
Nested margin collapse

But, the moment the two margins are separated -- by a border or by preceding content in the container, for example -- the margins no longer touch, so they no longer collapse.

EG, just a little, non-breaking white-space , like so:

<div id="outer">&nbsp;
    <div id="inner">
        A
    </div>
</div>

kills the collapse : (See that at jsFiddle.)
Nested margin doesn't collapse

Using a border, instead of leading text : (Fiddle)
no-collapse, border

like image 142
Brock Adams Avatar answered Sep 18 '22 16:09

Brock Adams


A diagram may help:

Illustration of collapsing margins for nested divs

In case it wasn't obvious: blue = outer div, red = inner div; I've drawn them with constant height and horizontal positioning. You can work out what happens if the height is fitted to the contents etc.

The "Before collapsing" column shows what you get if the margins aren't considered adjacent, e.g. if you draw the border of the blue/outer div; but if there is no border, then you get the "After collapsing" column. The top row switches the two margins around from the example, because I think the behaviour in this case is more intuitive; the bottom one shows the example at howtocreate and is consistent with the top row.

like image 33
Brian L Avatar answered Sep 17 '22 16:09

Brian L