CSS margin-top of affects parent’s margin

Thank you all for your answers, @gaurav5430 posted a link with a very precise definition that I would like to summarize into this answer. As of why they decided that this elements should behave like this it is still unclear for me; but at least we were able to find a rule that applies to collapsing margins:

“In simple terms, this definition indicates that when the vertical margins of two elements are touching, only the margin of the element with the largest margin value will be honored, while the margin of the element with the smaller margin value will be collapsed to zero

Basically in our example on the original question, the biggest margin-top is the one of the <h1> therefore taken and applied to the parent <div>.

This rule is cancelled for:

  • Floated elements
  • Absolutely positioned elements
  • Inline-block elements
  • Elements with overflow set to anything other than visible (They do not collapse margins with their children.)
  • Cleared elements (They do not collapse their top margins with their parent block’s bottom margin.)
  • The root element

That is the reason why overflow:hidden solved the issue.

Thanks @gaurav5430; here is the reference: http://reference.sitepoint.com/css/collapsingmargins

Also thanks to @Adrift that posted very good resources, although I found @gaurav5430’s answer more straight forward and easier to understand.

Leave a Comment