{"id":104,"date":"2005-06-16T13:54:28","date_gmt":"2005-06-16T11:54:28","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/archives\/2005\/06\/16\/essentials-of-css-hacking-for-internet-explorer\/"},"modified":"2011-05-19T11:18:43","modified_gmt":"2011-05-19T10:18:43","slug":"essentials-of-css-hacking-for-internet-explorer","status":"publish","type":"post","link":"http:\/\/www.maratz.com\/blog\/archives\/2005\/06\/16\/essentials-of-css-hacking-for-internet-explorer\/","title":{"rendered":"Essentials of CSS Hacking For Internet Explorer"},"content":{"rendered":"<p>The summary of <a href=\"http:\/\/web.burza.hr\/en\/\">our<\/a> <a href=\"http:\/\/www.nacional.hr\/\">latest project<\/a> client-side development brought to conclusion that there\u2019re  are really just a few essential Internet Explorer hacks. By careful structure planning, I managed to stripe down all hackery to a much less additional rules. Since <a href=\"http:\/\/www.microsoft.com\/\">they<\/a> are promising IE7 some time soon, more and more I think about secure CSS hacking. We surely don&#8217;t want our sites to be a mess in IE7 for it\u2019s quite possibly half-repaired <a href=\"http:\/\/blogs.msdn.com\/ie\/archive\/2005\/04\/22\/410963.aspx\">CSS support<\/a>.<!--more--><\/p>\n<h3>Conditional Comments<\/h3>\n<p>The alpha and the omega of IE hacking are IE\u2019s <a href=\"http:\/\/msdn.microsoft.com\/workshop\/author\/dhtml\/overview\/ccomment_ovw.asp\">conditional comments<\/a>. They are IE-only feature and they\u2019re not supported by any other browser. For other browsers they are just an ordinary comments and therefor, they are safe to use.<\/p>\n<p>The typical usage is as follows:<\/p>\n<pre>&#60;!--[if IE]&#62;\r\n    do something\r\n&#60;![endif]--&#62;<\/pre>\n<p>Untill now I used to write something like above, which applies to all versions of Internet Explorer, i.e. 5.01, 5.5 and 6.0, but since the latest announcements, I started applying the following condition:<\/p>\n<pre>&#60;!--[if lte IE 6]&#62;\r\n    do something\r\n&#60;![endif]--&#62;<\/pre>\n<p>which means: \u201cif this is Internet Explorer <strong>less than or equal to version 6<\/strong>, do something\u201d. My thoughts are\u2013if they keep conditional comments feature in IE7, then the browser will ignore this, since it\u2019s version designation number is 7. On the other hand, if they abandon that feature, the browser will assume that this is just another HTML comment.<\/p>\n<p>When I work on a layout I usually place all hacks for some selector immediately after its\u2019 default rule. This way, changes can be done quickly and without searching for the corresponding hack in other places.<\/p>\n<p>After I\u2019m done with layout, I like to go through all of the CSS files once again and optimize everything from short-hand properties to assigning the same rule for multiple selectors. At that point all hacks are removed to separate file(s), so the main CSS is clean and tidy. This separate file is then called in the header section of a file within conditional comments.<\/p>\n<pre>&#60;!--[if lte IE 6]&#62;\r\n    &#60;link rel=\"stylesheet\" type=\"text\/css\" href=\"ie_hacks.css\" \/&#62;\r\n&#60;![endif]--&#62;<\/pre>\n<p>While still in main CSS file, hacked selectors start with <code>* html<\/code>. This is known as the \u2018star-HTML\u2019 hack. Standard compliant browsers ignore this selector, because there\u2019s actually  no elements above <code>html<\/code> in a document tree. Luckily, IE doesn\u2019t know that and we\u2019re safe to use this flaw when applying IE specific hacks. Once we move hacks to a separate file and call it in a document with conditional comments, it\u2019s safe to remove the <code>* html<\/code> part.<\/p>\n<p>Further on in the text, assume that we are dealing with separate file with IE hacks only.<\/p>\n<h3>Backslash hack for IE 5.x broken box model<\/h3>\n<p>A combination of width and padding on the same element is very well known to produce broken layouts in IE 5.x. Box model hack is widely used and can be stripped down to a few lines.<\/p>\n<pre>someselector {\r\n    padding: 10px;\r\n    width: 200px;\r\n    w&#92;idth: 180px;\r\n    height: 200px;\r\n    heigh&#92;t: 180px;\r\n}<\/pre>\n<p>This will give as an element which is 200px wide, 200px high and with 10px paddings in both IE 5.x and IE 6.<\/p>\n<h3>If floated and with margin, display: inline<\/h3>\n<p>Everything that is floated and has any margin larger than zero, should have additional rule <code>display: inline;<\/code>. Simple as that.  More about the \u2018Holly hacks\u2019 read at <a href=\"http:\/\/www.positioniseverything.net\/\">P.I.E.<\/a>.<\/p>\n<h3>Overflow problems<\/h3>\n<p>Italic font style in any IE version will enlarge parent element horizontally. It\u2019s usually just a few pixels, but it could be nasty and ugly few pixels. Simple solution is in the following rule:<\/p>\n<pre>someselector {\r\n    overflow-x: hidden;\r\n}<\/pre>\n<p>I tend to apply that rule to every major column in a layout, especially in the period right after site launch. For the first week or two, when the large amounts of content are added and removed day and night by the site\u2019s editors, it\u2019s better to prevent content slips out of it\u2019s boxes. A small digression\u2013editors are sometimes inexperienced, but it\u2019s not their job to know every HTML element and that\u2019s why adequate support in their few first attempts is essential. There\u2019s no worst for the guy who manage the content on his company\u2019s brand new, ultra-modern and expensive web site, than the massive layout breakage when the site is still fresh and under the eyes of CEO. It\u2019s our responsibility to keep his confidence intact.<\/p>\n<h3>Font size in tables in IE 5.x<\/h3>\n<p>Font rules set in <code>html<\/code> or <code>body<\/code> element are ignored in tables in case of IE 5.x. Again, simple addition will take care of it.<\/p>\n<pre>body {\r\n    font-size: 62.5%;\r\n}\r\ntable {\r\n    font-size: 1em;\r\n}<\/pre>\n<p>Later in a process, you can change rules according to a particular table\u2019s needs.<\/p>\n<h3>To conclude&#8230;<\/h3>\n<p>CSS hacks are necessary evil, but with defensive approach, we can make sure they safely co-exist with default CSS rules, even in future browsers. What are your thoughts?<\/p>\n<h3>Related Reading<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.positioniseverything.net\/explorer.html\">Position Is Everything<\/a><\/li>\n<li><a href=\"http:\/\/tantek.com\/CSS\/Examples\/boxmodelhack.html\">Tantek\u2019s Box Model Hack<\/a><\/li>\n<li><a href=\"http:\/\/www.andybudd.com\/archives\/2003\/08\/avoiding_tanteks_box_model_hack\/\">Avoiding Tantek\u2019s \u201cBox Model Hack\u201d<\/a><\/li>\n<li><a href=\"http:\/\/www.informit.com\/articles\/printerfriendly.asp?p=170511\">Strategies for Long-Term CSS Hack Management<\/a><\/li>\n<li><a href=\"http:\/\/www.stylegala.com\/resources\/css_hacks.htm\">CSS hacks | Stylegala<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>How to apply CSS hacks safely<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[71],"_links":{"self":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":0,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}