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.

8 Comments

  1. On October 18, 2008
    10:34PM

    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. On October 18, 2008
    10:37PM

    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. On January 02, 2009
    12:08PM

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

22 Responses Elsewhere

  1. On June 15, 2008
    6:12PM

    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. On June 16, 2008
    7:51PM

    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. On June 30, 2008
    7:45AM

    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. On August 25, 2008
    12:06PM

    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. 使用CSS修正一切:20多个常见Bug及其修正方法 | 前端观察 said:

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

  12. 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. [...]

  13. 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 [...]

  14. 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/ [...]

  15. 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. [...]

  16. 7 IE Bugs & Hacks | Fresh said:

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

  17. 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 [...]

  18. On April 02, 2009
    11:02PM

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

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

  19. On April 07, 2009
    9:04PM

    » 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. [...]

  20. 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 [...]

  21. 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: [...]

Leave a Comment