CSS Firefox top-margin Extra Space



Firefox renders CSS margin-top in such a way that if there are two elements, one inside another, and you set margin-top for the inner element, the outer element collapses the margin-top amount of height. So you may see extra ‘padding’ or ’spacing’ at top of the outer element.

CSS:
.outer {background-color:#FF9900; border:1px solid black; width:150px; height:150px; }
.middle{width:100px; height:100px; background-color:#666666; }
.inner {width:50px; height:50px; background-color:#99CC66; margin-top:20px;}


inner


The solution is to add either a padding-top:

CSS:
.outer-b {background-color:#FF9900; border:1px solid black; width:150px; height:150px; }
.middle-b {width:100px; height:99px; background-color:#666666; padding-top:1px; }
.inner-b {width:50px; height:50px; background-color:#99CC66; margin-top:19px;}

inner


or a border-top:

CSS:
.outer-c {background-color:#FF9900; border:1px solid black; width:150px; height:150px; }
.middle-c {width:100px; height:99px; background-color:#666666; border-top:1px solid #666666; }
.inner-c {width:50px; height:50px; background-color:#99CC66; margin-top:19px;}

inner


A clean version of the html file is here.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

,

  1. #1 by Shane McCallum on November 12, 2008 - 2:01 pm

    Helpfull hint. I have experienced this many times during deveopment but never truly knew the reason why I needed to implement a work-around. Thanks.

  2. #2 by revert on April 15, 2009 - 9:06 am

    me too. Thanx:)

  3. #3 by admin on April 15, 2009 - 9:46 am

    you’re welcome, guys, I am glad it helps!

  4. #4 by Luis on February 9, 2010 - 11:55 am

    That helped me a lot. But I’m still confused why does FF add this extra space.

(will not be published)