Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item with margin-top has margin outside of containing box

Tags:

html

css

margins

I have something to this effect:

#div {
   background: green; 
   width: 200px;
   height: 100px;
}

h1 {
   margin-top: 100px;
}

(And obviously there is some HTML to go with it.)

Now when viewed in the browser the h1 shows at the very top of the #div and the 100px margin goes on top of the #div.

Can anyone suggest a reason?

(The code is a little too complicated to insert the relevant parts so if no-one can give me an answer then I will post it and maybe someone will spot an error or something.)

like image 236
Chaim Avatar asked Aug 03 '11 16:08

Chaim


Video Answer


1 Answers

This is not related to the H1 tag. This is what we call margin collapse.

You can find a post about this subject here : http://reference.sitepoint.com/css/collapsingmargins

you have several solutions :

  • use padding instead
  • add overflow: auto on your parent div
  • add transparent top border to the parent div
like image 133
Jerome Cance Avatar answered Sep 20 '22 13:09

Jerome Cance