Bug Fix: IE Double Margin Float Bug

The double-margin float bug has been a source of irritation for CSS-loving web designers for years. The bug first became a major problem in IE5, when CSS started to become increasingly popular, and persisted through IE6. And, while an easy (if mysterious) fix has been known for quite some time now, it occurs to me that perhaps not everyone knows about it. So I thought it couldn’t hurt to toss another explanation out there.

So what is the double-margin float bug? It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. In other words, if you were to float an element to the left and give it a 20-pixel left margin, in IE the margin would actually be 40 pixels wide. It only happens when the margin is in the same direction as the float, but it happens to both left and right floats. At least IE is consistent in its inconsistency.

Obviously, this can be a pretty annoying problem. If, for example, you use floats to create columns (as I tend to do), this little bug can throw off your entire layout in Internet Explorer. Let’s say you create a wrapper div 800px wide, and then create two columns inside. You decide to float your columns left to get them next to one another, and then you give your leftmost column a margin to push it away from the edge of the wrapper. Assuming you’ve done your math right, this should work perfectly in every browser but IE. The ol’ blue beast, however, has doubled your left margin and thrown your columns completely out of whack.

Happily, the fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. Seriously: that’s it. So you simply go from something like this:

#content {
	float: left;
	width: 500px;
	padding: 10px 15px;
	margin-left: 20px; }

To something like this:

#content {
	float: left;
	width: 500px;
	padding: 10px 15px;
	margin-left: 20px;
	display: inline; }

And why does a display property fix our margin problem? Really, your guess is as good as mine. In all truthfulness, applying a display property to a float should do exactly nothing (unless it’s display: none, that is). Floats are by definition block-level elements, and cannot become inline elements. And even IE knows this – after you apply this rule all browsers including Internet Explorer will continue to treat your floats like block-level elements. But now IE will also start behaving itself when it comes to your margins.

However, that’s also what makes this fix so nice: you can apply it to your element and not have to worry about what other problems that might cause down the line. Floats can’t be inline elements, so the property does nothing harmful.

As I said previously, this fix has really been around for some time. You can read more about it at Position Is Everything, which also gives a bit of the history of the bug and some of the old-school workarounds web designers used before this little fix was discovered.

9 Comments

  1. Johny said:

    WOW! I should have come across this post before, it could have saved me 2 hours of trying to figure out the problem… Thank you very much!!! they need to disappear ie6 from every computer.

  2. Css website design layout said:

    This is great! Thanks for posting this. Saved me a lot of time.

  3. Moslem said:

    Oh! very thanks, i have this problem for many projects, now i van design faster!

  4. Satish jat said:

    This is very usefull tag for double floating margin error

    and a alternating tag this

    .submit{
    float:left; margin-left:10px;
    }

    replace this tag for alternating solution

    .submit{
    float:left; margin: 0px 0px 0px 10px;

    this is alternating solution of double floating margin bug

    Satish jat
    }

  5. Carlos said:

    That is rediculous. That is so easy I can’t believe I did not know it before hand. Stupid margin doubling in IE6 has been a vice of mine for a while. I have to keep this in lock and key. Thank you very much!

  6. On April 19, 2009
    1:15PM

    Alessandro said:

    Instead of using display:inline there is an easier and safer solution: using this simple js bug fix http://www.programmatorephp.it/jquery/ie6-double-margin-hack.php
    Other solutions have some side effects.

  7. On June 08, 2009
    3:32AM

    Ryan said:

    Thanks for the quick tip. You saved this CSS newbie a lot of mental anguish. :)

  8. On June 26, 2009
    11:18AM

    Graham said:

    Thanks for the fix. Thankfully this isn’t needed in IE7 and above..

  9. Rob said:

    Great stuff. Personally I’ve alwasys used conditional comments, didn’t know about the display trick.

35 Responses Elsewhere

  1. Using CSS to Fix Anything: 20+ Common Bugs and Fixes said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  2. CSS, IE bug | 亿顺之家 said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  3. On June 17, 2008
    10:56AM

    Using CSS to Fix Anything: 20+ Common Bugs and Fixes | André Friedrichs' Web site said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  4. All About Floats - CSS-Tricks said:

    [...] Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. [...]

  5. On July 03, 2008
    12:20AM

    Using CSS to Fix Anything: 20+ Common Bugs and Fixes at Serradinho Blog said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  6. On July 11, 2008
    12:20PM

    CSS float: considerações, dicas e macetes para bons layouts na web | desenvolvimento para web said:

    [...] Outra coisa para se lembrar quando se lida com IE6 é que, se você aplicar uma margem (CSS “margin”) no mesmo sentido que o float (”left” ou “right”), ela deve ser o dobro da margem. [...]

  7. The Geek in ROC » Blog Archive » All About Floats said:

    [...] Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. [...]

  8. All About Floats said:

    [...] Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. [...]

  9. Colección Css (tutoriales): Layout, tables, forms, buttons… — WYDBLOG said:

    [...] 22. Bug Fix: IE Double Margin Float Bug [...]

  10. 20 CSS-решений распространенных багов и проблем при верстке сайта. Блог для вебмастеров said:

    [...] Двойной внешний отступ элемента (IE Double Margin Float Bug) – у блочного элемента с прописанными [...]

  11. laura-lopes.com » Blog Archive » Como usar float em CSS said:

    [...] Outra coisa para se lembrar quando se lida com IE6 é que, se você aplicar uma margem (CSS “margin”) no mesmo sentido que o float (”left” ou “right”), ela deve ser o dobro da margem. [...]

  12. Jun Blog » Using CSS to Fix Anything: 20+ Common Bugs and Fixes said:

    [...] Bug Fix: IE Double Margin Float Bug – It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a [...]

  13. IE bugs? Fix them with CSS and Javascript at DCNY BLOG said:

    [...] IE Double Margin Float Bug Solution: display: inline; http://www.cssnewbie.com/double-margin-float-bug/ [...]

  14. 11 steps to make your website cross browser compatible said:

    [...] Not just this, Internet explorer 5.5/6.0 will add a little spacing on the top of the bulleted list even when you’ve set the margins and padding correctly. And if you don’t know about Double margin float bug on IE 6.0 then do check it out here. [...]

  15. 7 IE Bugs & Hacks | Fresh said:

    [...] #5: Double Margin [...]

  16. Renoviert! » Guru 2.0 said:

    [...] der ja laut Microsoft in der Version 7 behoben sein sollte. War/Ist er aber nicht. Bei cssNewbie.com habe ich einen netten Tip dazu gefunden, aber geholfen hat er eigentlich nicht. Die [...]

  17. On April 02, 2009
    11:02PM

    purecss » Blog Archive » 使用CSS修正一切:20多个常见Bug及其修正方法 said:

    [...] 1- Bug修正:IE双倍Margin bug #content { [...]

  18. » Estilo Float (CSS Style Float) | Como Criar Sites | Aprenda grátis como fazer seu site said:

    [...] E para corrigir algum bug do IE com relação a margem colocada na mesma direção qual o elemento flutue, veja Bug Fix: IE Double Margin Float Bug. [...]

  19. On May 12, 2009
    1:11AM

    蝴蝶飞过» Blog Archive » Using CSS to Fix Anything: 20+ Common Bugs and Fixes said:

    [...] Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a [...]

  20. On July 01, 2009
    12:55PM

    Using CSS to Fix Anything: 20+ Common Bugs and Fixes | www.my2tech.com said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  21. All About Floats | said:

    [...] when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set display: inline on the float, and don’t worry it will remain a block-level [...]

  22. On July 10, 2009
    11:50AM

    All About Floats | E Marketing, Web Design, Web Develop, SEO and SEM Egypt Blog said:

    [...] when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set display: inline on the float, and don’t worry it will remain a block-level [...]

  23. Using CSS to Fix Anything: 20+ Common Bugs and Fixes | Webmaster Tips said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  24. Using CSS to Fix Anything: 20+ Common Bugs and Fixes | Webmaster Zone - Cyooh.com said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

  25. All About Floats | PHP Hosts said:

    [...] when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set display: inline on the float, and don’t worry it will remain a block-level [...]

  26. 关于浮动| CSS| 前端观察 said:

    [...] 双倍边距bug处理 IE6 时,另一个需要记住的事情是,如果在和浮动方向相同的方向上设置外边距(margin),会引发双倍边距。快速修正:给浮动设置 display:inline; 而且不用担心,它依然是块级元素。 [...]

  27. Nuthin » Blog Archive » [转]All About Floats said:

    [...] when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set display: inline on the float, and don’t worry it will remain a block-level [...]

  28. CSS浮动属性Float详解 | 帕兰映像 said:

    [...] 双倍边距bug处理 IE6 时,另一个需要记住的事情是,如果在和浮动方向相同的方向上设置外边距(margin),会引发双倍边距。快速修正:给浮动设置 display:inline; 而且不用担心,它依然是块级元素。 [...]

  29. [转载]CSS浮动属性Float详解 said:

    [...] 双倍边距bug处理 IE6 时,另一个需要记住的事情是,如果在和浮动方向相同的方向上设置外边距(margin),会引发双倍边距。快速修正:给浮动设置 display:inline; 而且不用担心,它依然是块级元素。 [...]

  30. Cross Browser Web Development | Van SEO Design said:

    [...] As your skills improve you’ll find you only need to adjust a few things for IE. Maybe one div is displaying further from the right edge than you intended due to the double float issue. [...]

  31. RIA Worlds » 关于float解读和相关资料整理 said:

    [...] 双倍边距bug处理 IE6 时,另一个需要记住的事情是,如果在和浮动方向相同的方向上设置外边距(margin),会引发双倍边距。快速修正:给浮动设置 display:inline; 而且不用担心,它依然是块级元素。 [...]

  32. CSS修正20多个常见Bug的方法 | Web启点 said:

    [...] 1- Bug修正:IE双倍Margin bug [...]

  33. Using CSS to Fix Anything: 20+ Common Bugs and Fixes | qface & sowmo sky said:

    [...] 1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this: [...]

Leave a Comment