Skip to content Skip to sidebar Skip to footer

Why Is There A Gap Between My Image And Its Containing Box?

When my browser renders the following test case, there's a gap below the image. From my understanding of CSS, the bottom of the blue box should touch the bottom of the red box. But

Solution 1:

Inline elements are vertically aligned to the baseline, not the very bottom of the containing box. This is because text needs a small amount of space underneath for descenders - the tails on letters like lowercase 'p'. So there is an imaginary line a short distance above the bottom, called the baseline, and inline elements are vertically aligned with it by default.

There's two ways of fixing this problem. You can either specify that the image should be vertically aligned to the bottom, or you can set it to be a block element, in which case it is no longer treated as a part of the text.

In addition to this, Internet Explorer has an HTML parsing bug that does not ignore trailing whitespace after a closing element, so removing this whitespace may be necessary if you are having problems with Internet Explorer compatibility.

Solution 2:

Because the image is inline it sits on the baseline. Try

vertical-align: bottom;

Alternately, in IE sometimes if you have whitespace around an image you get that. So if you remove all the whitespace between the div and img tags, that may resolve it.

Solution 3:

line-height: 0; on the parent DIV fixes this for me. Presumably, this means the default line-height is not 0.

Solution 4:

display: block

in the image fixes it as well, but probably breaks it in other ways ;)

Solution 5:

Remove the line break before the tag, so that it directly follows the tag with no blanks between it.

I don't know why, but for the Internet Explorer, this works.

Post a Comment for "Why Is There A Gap Between My Image And Its Containing Box?"