Forum Moderators: not2easy

Message Too Old, No Replies

IE7 min-height bug

Tripped inside absolutely positioned parent

         

thesheep

12:46 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



I seem to have found an annoying IE7 bug. It is tripped under fairly specific circumstances - see code below. In IE7 as you resize the browser window you will frequently see the red background of #box showing through on the right - as if it has some phantom padding. You may need to resize the window a few times before you see the bug.

This only occurs with a minimum height applied to #innerbox and with another block element above the box, when the whole thing is contained inside a container set with position: absolute.

You might need this kind of setup if you have a box with rounded corners, for example, where the corners are put on the background image - this bug will destroy the box layout. So in this kind of layout it seems that IE7's new min-height capabilities are not useable.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
#main {
position: absolute;
top: 0; left: 0;
}

#box {
background-color: red;
margin: 0 200px;
padding: 0;
}

#innerbox {
background-color: blue;
min-height: 100px;
padding: 0; margin: 0;
}

</style>
</head>
<body>

<div id="main">

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse id tellus eu arcu fringilla dapibus. Ut eget mauris. Sed faucibus rhoncus ipsum. Mauris quis nunc quis eros elementum sagittis. In eget eros. Suspendisse tempus, quam nec tincidunt rutrum, metus ipsum convallis dui, quis bibendum augue diam eu erat. Praesent a nibh. Integer lobortis porta lorem. Vivamus congue est eu justo. Nulla lacinia viverra risus. Mauris facilisis tellus ut dui. Donec eu dolor. In hac habitasse platea dictumst. Pellentesque leo. Donec quam. Praesent nunc ante, imperdiet non, imperdiet sed, tristique ut, tellus.</p>

<div id="box">
<div id="innerbox">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse id tellus eu arcu fringilla dapibus. Ut eget mauris. Sed faucibus rhoncus ipsum. Mauris quis nunc quis eros elementum sagittis. In eget eros. Suspendisse tempus, quam nec tincidunt rutrum, metus ipsum convallis dui, quis bibendum augue diam eu erat.</p>
</div><!-- end#innerbox -->
</div><!-- end#box -->

</div><!-- end#main -->
</body>
</html>

SuzyUK

2:33 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi thesheep, well spotted, and thanks for the nice code sample - it's one to add to the IE7 thread when it gets built ;)

I think min-height will throw a few surprises as it is a hasLayout triggering property, but it doesn't mean it can't be used, just that some of the usual hasLayout fixes might have to come into play

min-height is triggering hasLayout to true on #innerbox, you can actually trigger the same quirky behaviour using zoom: 1; or a width (which might be needed anyway), instead of min-height on #innerbox

IE can't count, remember that! so this is causing a hasLayout error - why?

now #innerbox has "layout" it is responsible for creating the rectangle for it's content however that means it has to do sums to calculate it's width, so it has to know the width of something in it's ancestry in order to do that but it doesn't.

there are actually three ways to fix this: and two are related to the absolutely positioned div.

1. The usual hasLayout fix, give the parent some responsibility. Trigger layout to true on #box, e.g. you could use the same min-height.

The next two are slightly different and seem to prove the "let's help IE count" theory.. the error seems to stem right back to the fact that the absolutely positioned main box doesn't know its width either so fixes 2 and 3 involve clarifying things a little for IE
2. set right: 80px;
or
3. width: 80%
on the #main div, you can adjust those values to suit whichever width you want, but either of those also seem to stabilise the situation.

basically any time IE not just 7 has to count to calculate a width - in #box it's because of the margins - and then a layouted box is involved it can throw a display wobbly!

not sure if solution 2/3 would still work if the nesting was more complex, I didn't test that

Suzy

thesheep

4:59 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



Thanks Suzy, that helps me understand a bit more about what's going on behind the scene. The whole IE 'hasLayout' thing makes my head go foggy, so I think I'll have to read up on it.

Following your advice, I played around with a few options, and in the end the best thing seems to be to add width: 100% to #main. My layout is a lot more complex than the test page, and luckily that works fine on the real page (adding min-height to #box didn't work so well because then the whole width of #box became erratic).

It's nice when the solution is so simple and doesn't involve some annoying parsing hack or something. Thanks again.

[Edit]
Well, spoke a bit too soon. Adding width: 100% produces some odd layout behaviour in Firefox, so still don't have a perfect solution. But at least I have some things to play around with.