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
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

About Shi Chuan

I am a web developer.
This entry was posted in CSS Hacks and tagged , . Bookmark the permalink.

8 Responses to CSS Firefox top-margin Extra Space

  1. 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. revert says:

    me too. Thanx:)

  3. admin says:

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

  4. Luis says:

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

  5. Nelson says:

    Cheers mate really helpful

  6. davina says:

    thank you — i was working on a wordpress site that was driving me crazy with this anomaly in firefox!

  7. Scott says:

    Outstanding, thank you so much for this.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">