CSS: Why "vertical-align: middle" does not work?

Viewed 14638

Consider the following example: (live demo here)

HTML:

<a><img src="http://img.brothersoft.com/icon/softimage/s/smiley.s_challenge-131939.jpeg" /></a>

CSS:

a {
    display: block;
    background: #000;
    line-height: 40px;  
}
img {
    vertical-align: middle;
}

The output is:

enter image description here

Why the image is not vertically centered ?

How could I fix that so it will work in all major browsers ?

Please don't assume any image size (like 32x32 in this case), because in the real case the image size is unknown.

8 Answers

If you know the vertical size of the element and the line height, then you can vertically center it by using vertical-align: top plus a top margin.

For illustration, I created: http://jsfiddle.net/feklee/cXUnT/30/

Related