Why did the width collapse in the percentage width child element in an absolutely positioned parent on Internet Explorer 7?
The parent div
needs to have a defined width
, either in pixels or as a percentage. In Internet Explorer 7, the parent div
needs a defined width
for child percentage div
s to work correctly.
Here is a sample code. I think this is what you are looking for. The following code displays exactly the same in Firefox 3 (mac) and IE7.
#absdiv {
position: absolute;
left: 100px;
top: 100px;
width: 80%;
height: 60%;
background: #999;
}
#pctchild {
width: 60%;
height: 40%;
background: #CCC;
}
#reldiv {
position: relative;
left: 20px;
top: 20px;
height: 25px;
width: 40%;
background: red;
}
<div id="absdiv">
<div id="reldiv"></div>
<div id="pctchild"></div>
</div>
IE prior to 8 has a temporal aspect to its box model that most notably creates a problem with percentage-based widths. In your case here an absolutely positioned div
by default has no width. Its width will be worked out based on the pixel width of its content and will be calculated after the contents are rendered. So at the point, IE encounters and renders your relatively positioned div
its parent has a width of 0 hence why it itself collapses to 0.
If you would like a more in-depth discussion of this along with lots of working examples, have a gander here.