{"id":142,"date":"2008-06-02T08:00:11","date_gmt":"2008-06-02T13:00:11","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=142"},"modified":"2008-06-02T08:00:11","modified_gmt":"2008-06-02T13:00:11","slug":"double-margin-float-bug","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/double-margin-float-bug\/","title":{"rendered":"Bug Fix: IE Double Margin Float Bug"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/06\/doublemargin-400.gif\" alt=\"\"  width=\"400\" height=\"200\" class=\"alignnone size-full wp-image-144\" \/><\/p>\n<p>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\u2019t hurt to toss another explanation out there.<\/p>\n<p>So what is the double-margin float bug? It\u2019s an Internet Explorer-exclusive bug wherein an element that is floated \u2013 and given a margin in the same direction as the float \u2013 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.<\/p>\n<p>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\u2019s 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\u2019ve done your math right, this should work perfectly in every browser but IE. The ol\u2019 blue beast, however, has doubled your left margin and thrown your columns completely out of whack.<\/p>\n<p>Happily, the fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. Seriously: that\u2019s it. So you simply go from something like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#content {\n\tfloat: left;\n\twidth: 500px;\n\tpadding: 10px 15px;\n\tmargin-left: 20px; }<\/pre>\n<p>To something like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#content {\n\tfloat: left;\n\twidth: 500px;\n\tpadding: 10px 15px;\n\tmargin-left: 20px; \n\tdisplay: inline; }<\/pre>\n<p>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\u2019s display: none, that is). Floats are by definition block-level elements, and cannot become inline elements. And even IE knows this \u2013 after you apply this rule all browsers <em>including Internet Explorer<\/em> will continue to treat your floats like block-level elements. But now IE will also start behaving itself when it comes to your margins.<\/p>\n<p>However, that\u2019s 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\u2019t be inline elements, so the property does nothing harmful.<\/p>\n<p>As I said previously, this fix has really been around for some time. You can read more about it at <a href=\"http:\/\/www.positioniseverything.net\/explorer\/doubled-margin.html\">Position Is Everything<\/a>, 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.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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. Happily, the fix is extremely simple.  [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/double-margin-float-bug\/\" title=\"Click to read 'Bug Fix: IE Double Margin Float Bug'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[170,178,187],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/142"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}