Handling Internet Explorer 6 Bugs

Internet Explorer 6 has a long history of CSS bugs, especially (and unfortunately) when it comes to float-based layouts. These bugs can affect the placement of floats and the overall width allotted to floated elements. If you're lucky, you may just get a slightly annoying difference in how your web page looks in Internet Explorer versus other browsers. At worst, these bugs can cause significant display problems like the float drops discussed in the previous section. This section tells you the most common problems and how to get around them.

Note

See the box on :after to decide how much you need to be worried about Internet Explorer 6.

Internet Explorer 6 and earlier sometimes doubles the size of a margin you've applied to a floated element. The problem occurs only when the margin is in the same direction as the float—a left margin on a left-floated element or a right margin on a right-floated element. In Figure 12-13, there's a left-floated sidebar holding the site's navigation. To add a bit of space between it and the left edge of the browser window, the sidebar has a left margin of 10 pixels.

Most browsers, including Internet Explorer 7 and 8, Safari, and Firefox (Figure 12-13, top), add the requested 10 pixels of space. However, Internet Explorer 6 (bottom) doubles that margin to 20 pixels. Even with relatively small margins like 10 pixels, the visual difference is significant. Furthermore, if the layout is very tight, with precisely measured floated elements sitting side by side, then the doubled margin can easily trigger a float drop (Preventing Float Drops).

The solution is simple: Add display:inline; to the CSS style for the floated element:

#sidebar {
  float: left;
  margin-left: 10px;
  width: 160px;
  display: inline;
}

In this case, the display property doesn't do anything except fix IE's bug. In fact, the only reason it's there is to force the sidebar element to "have layout," as described in the box on Tutorial: Multiple-Column Layouts, so you can alternatively use zoom:1 instead of display:inline. Floated elements are always block-level elements, and changing a style's display to inline won't alter that. (See Adding Borders for more on the display property.) However, even though this added style declaration doesn't adversely affect any other browsers, you may want to put it in an IE-only style using the * html hack:

#sidebar {
  float: left;
  margin-left: 10px;
  width: 160px;
}
* html #sidebar {
  display: inline;
}

Internet Explorer 6 and earlier inserts an additional three pixels of space between a floated column and a non-floated column. The exact placement of that gap depends on a couple of conditions:

A few more bugs plague float-based layouts in Internet Explorer 6. Many of them are so rare you may never come across them in your web design projects. But just in case, here are a couple of weird things that can happen when viewing a page in IE 6 or earlier.