1

I have an image in a div, lets say like so:

<div>
<img src="myimage.png">
</div>

The style for the div, is line-height:36px;

The image's height is 12px

This code lives on many web sites (think a button / badge etc.) - it essentially always works. But occasionally the div collapses around the image, and I'm trying to figure out why.

If I place an &nbsp; next to the image, the line-height begins to work again.

Setting the image to inline, giving the div a specific font-size, did not seem to make a difference

Elliot
  • 13,580
  • 29
  • 82
  • 118

3 Answers3

1

Apply a new style to div :

<div style="display:list-item;"></div>

May this helps you.

keshu_vats
  • 452
  • 6
  • 45
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
1

Probably it's because the img has been set to display:block. In such a case, there is nothing left to form a line box. If there's no line box, there's nothing for line-height to be applied to.

Possibly floating the img may also be confusing the matter, because that will also leave nothing to create the line box. But in this situation, the div will collapse to 0 height, rather than wrap just around the image.

See http://jsfiddle.net/p6Jzt/1/

Alohci
  • 78,296
  • 16
  • 112
  • 156
0

try div { font-size: 0; }, its height must be same as its line-height

Why are images centered vertically with `line-height` positioned 2 pixels below where they should be?

Community
  • 1
  • 1
mahish
  • 975
  • 7
  • 15