Friday, September 25, 2009

IE6 Bug: Relative Box After Float Doesn't Show

Consider the following code where you have a floated element (in this case a label), followed by a div with position: relative, containing an input field with a width: 100%.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
.label { float: left; }
.div { position: relative; }
.input { width: 100%; }
</style>
</head>
<body>
<label class="label">Label</label>
<div class="div">
<input class="input" type="text"/>
</div>
</body>
</html>
The issue is that on IE6, the input field doesn't show:

This bug has been resolved in IE7, and you can easily work around it in IE6 by forcing the box to "have layout" (in IE, elements with position: absolute have layout, but elements with position: relative don't necessarily have layout). My favorite way for doing so it to add a *zoom: 1 on the box, which will give you the expected result: