views:

214

answers:

2

I need a div with a height of exactly 1em minus 1px. This can be achieved in most browsers like this:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <style type="text/css">

        .helper {
            /* background-color: black; */

            position: absolute;
            top: 5em;
            left: 5em;

            width: 2em;
            height: 1em;
        }
        .target {
            background-color: #89f;

            position: absolute;
            top: 0;
            bottom: 1px;

            width: 100%;
        }           
    </style>
</head>
<body>
    <div class="helper">
        <div class="target"></div>
    </div>
</body>
</html>

The "target" div has the desired height. The problem is, that this doesn't work in IE6, because it ignores the bottom attribute, when top is set (a well known problem).

Is there a workaround for IE6 (maybe with multiple nested divs, with borders/paddings/margins/whatever), or will JavaScript be the only solution?

Please note, that I can't use Quirks Mode.

+1  A: 

Is it required by the client? If not then just abandon IE6 and hacks for this crappy/old browser.

dario-g
+2  A: 

Does the target div have to be physically 1px smaller or just display 1px smaller?

The easiest way would be to add in an "ie6 only" stylesheet:

.helper {overflow:hidden;}
.target {top:auto;}

This will display target as 1em - 1px but its real height is 1em with the top 1px is hidden.


IE6 is flaky when it comes to absolute positioning support.

Another solution, instead of the code above, would be to add in an "ie6 only" stylesheet:

.target {position:static;margin:-1px 0 1px 0;}

I see you got the absolute positioned solution to work. Great!

Emily
Thanks Emily. Very interesting, and this might go into a good direction! However, I just tried it, and the div isn't just one pixel smaller but multiple pixels (I'd say 3 or 4). Any ideas?
Chris Lercher
@Emily: Okay, I found out, that this was due to another IE6 problem with the additional div I used as a ruler to measure the height [IE6 doesn't like empty divs, and so it gave it a few pixels of extra height, which of course makes a lot of sense...] This means: Your solution is correct, it works :-) It will require to add a little bit of a workaround, but I think I can manage that.
Chris Lercher
Chris - I added a static position solution if the absolute position keeps giving you trouble.
Emily
@Emily: Very nice, your second solution also works! (It just needs an additional `line-height: 1em;`) Now I'm spoilt for choice :-)
Chris Lercher