IE6 must die (and IE7 can go with it)
A rant about Internet Explorer 6 and 7, hasLayout, zoom: 1 and conditional comments, with the hacks I can now type in my sleep.
Today I spent four hours on a menu. A horizontal menu. Five links side by side. In Firefox, Chrome, Safari and Opera it was done in twenty minutes. The remaining three hours and forty minutes went to Internet Explorer 6 and 7.
I’m going to complain for a bit now. Feel free to click away.
The problem is called hasLayout#
Internet Explorer 6 and 7 have an internal concept called “hasLayout”. An element either has layout or it doesn’t. If it doesn’t, things happen. Floats that don’t get cleared. Backgrounds that vanish. Margins that double. Text that only shows up once you scroll over it.
You can’t “give” an element layout with a property of that name. No, you have to set some other property that happens to switch it on as a side effect. The most popular one:
.menu {
zoom: 1;
}
zoom isn’t standard CSS. In IE it does something with zooming, and at a value of 1 it does nothing at all except turn on hasLayout. It’s the CSS equivalent of thumping the side of a television. It works, nobody knows why, and you feel a little ashamed.
My standard set of hacks#
After ten months at the agency, I type all of this without thinking:
/* make a container wrap its floats */
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
zoom: 1; /* IE6 and IE7 */
}
/* inline-block in IE6/7: only works on elements that are inline by nature */
.menu li {
display: inline-block;
*display: inline; /* asterisk: only IE7 and below read this */
zoom: 1;
}
/* the infamous double margin float bug in IE6 */
.sidebar {
float: left;
margin-left: 20px;
display: inline; /* otherwise it becomes 40px in IE6 */
}
/* IE6 has no min-height, but height behaves like it there */
.box {
min-height: 200px;
_height: 200px; /* underscore: only IE6 reads this */
}
Look at that. An asterisk in front of a property. An underscore in front of a property. Those are literally parse errors we abuse on purpose, because one browser ignores them and another doesn’t. And that’s what we call “best practice”.
Conditional comments: the least ugly option#
What I do on every new site is pull the hacks out of the main stylesheet and into separate files, loaded with conditional comments:
<link rel="stylesheet" href="css/style.css" />
<!--[if IE 7]>
<link rel="stylesheet" href="css/ie7.css" />
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" href="css/ie6.css" />
<script src="js/DD_belatedPNG.js"></script>
<script>DD_belatedPNG.fix('.logo, .button');</script>
<![endif]-->
Other browsers just see an HTML comment. IE reads the condition and loads the file. The upside: your main CSS stays clean and valid, and the day IE6 really disappears you delete one block.
Even neater is the trick with classes on the <html> tag:
<!--[if lt IE 7]> <html class="ie6"> <![endif]-->
<!--[if IE 7]> <html class="ie7"> <![endif]-->
<!--[if gt IE 7]><!--> <html> <!--<![endif]-->
Then you can write .ie7 .menu li { ... } in your regular stylesheet, without an extra request and without asterisks.
And that DD_belatedPNG up there? That’s because IE6 can’t display transparent PNGs. You get a grey box around your logo unless you throw a JavaScript library at it. In 2010. It’s nearly 2011.
“But the client uses IE6”#
Yes. That’s the whole problem. A logistics company we’re building an intranet for still runs Windows XP with IE6 across the entire company, because their accounting package needs an ActiveX plugin. I can’t argue with that. That’s their reality.
But the bakery? The sports club? Their visitors are at home on a reasonably recent PC. When I checked the bakery’s Analytics, IE6 came in at just over 3%. And still I got an email: “the logo is crooked on my father-in-law’s computer”.
I suggested to our project manager that we add a line to our quotes: “Support for Internet Explorer 6 is available as an option at extra cost.” She liked the idea. I’m curious how many clients take it once there’s a price tag attached. My guess: none.
There is hope. Even Google started dropping IE6 support for some of its services this year, and the IE9 beta finally looks like a real browser. But IE7 still shows up in every Analytics report, and IE8 has quirks of its own. We’re not done yet.
In closing#
My tip for any junior reading this: test in IE from day one, not the night before launch. Set up a virtual machine with XP and IE6, and another one with IE7. It feels like a waste of time, but it’s less of a waste than four hours on a menu.
And if anyone knows why position: relative on a parent sometimes makes a child element disappear in IE7, please leave a comment. I already tried zoom: 1.