Forum Moderators: not2easy

Message Too Old, No Replies

Inline image negates line-height on container?!

Only effects IE6

         

penders

1:00 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With a small image inline with the text in a container, the line-height in the container is completely negated?! More noticable if the line-height is much larger than the font-size. Only seems to effect IE6. (I thought I'd noticed a reverse effect in IE7 when the line-height was smaller than the font-size; although I can't seem to repeat this at the moment.)

All other browsers (FF, Opera, IE7) render as expected. ie. The inline image has no effect on the line-height!

Initially testing with a Strict DOCTYPE, although the DOCTYPE makes no difference in this case.

Example...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<title>Test Page</title>
<style type="text/css">
body {
font-size:1.5em;
line-height:5em;
}
#container {
background-color:#fcc;
}
</style>
</head>
<body>
<div id="container">
Blah blah blah
</div>
</body>
</html>

The height of #container is not explicitly set, but in ALL (inc IE6) browsers this will match the line-height (5em). And the text is vertically centred in #container.

Now add an image...

<div id="container"> 
Blah blah blah <img src="smallimg.gif" width="10" height="8" alt="">
</div>

In IE6 the height of #container is reduced to just contain the text. The line-height has been ignored/negated?! ALL other browsers are uneffected; the height/line-height is still 5em. Explicitly setting line-height on the #container has no effect. Setting height:5em does correct the height of the container, but the line-height is still ignored (ie. the text remains at the top of #container).

Giving the #container hasLayout (ie. width) has no effect.

If the text spans several lines, a similar effect happens, but only on the line that contains the image and only if that line is the first or last in the container (I think).

What's going on? Am I missing something? IE bug? What bug? I'm not sure how I've not noticed this before; as I feel sure I've vertically centred a single line of text plus image in a container before... may be not?!

SuzyUK

2:06 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Penders, this is indeed a known bug (fixed in IE7 IIRC)

Search Phrases:
[Line-height / Replaced Element Bug]
[line-height problem with inline images]

some solutions are in the pages you will find

penders

6:06 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the quick reply SuzyUK and search phrases. (I couldn't find anything on here [webmasterworld.com], only external pages, so if anyone does have a link...) However, there doesn't seem to be any 'magic' workaround as it will depend on the particular situation. A brief summary of some workarounds might include:

- Wrap your image in other images of width 1px and a height equal to the line-height. (Or use an "empty inline-block (hasLayout) span, with height the desired line-height")

- Adjust the top and bottom margins of the image to match the line-height

- Use a background-image

(fixed in IE7 IIRC)

Only partially fixed in IE7 it seems. In the scenario given above it has, but it still a problem if the line-height is less than normal or if the image is taller than the line-height.