Alex Rivera | Logout

Item with margin-top has margin outside of containing box

Asked 2011-08-03T16:29:45.760
17

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.)

Edit
Report

1 Answer

17

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
answered 2011-08-03T16:43:48.897

Your Answer