Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Vertically Center Images on a Line?

What's the best way to center icons on a line when the images are smaller than the line height?

For example (styles inline for easier reading):

<div style="line-height: 20px">   <img style="width: 12px; height: 12px;"/>   Blah blah blah </div> 

Here's a jsFiddle example. This example also shows why vertical-align: middle does not work.

I want the img to be centered against the text of the div. That is, even if the text wrapped to multiple lines, the image would be centered against a single line. Ideally, the solution would not involve setting margings/paddings on the image and would work even if I didn't know the line-height.

Things I've read:

How do I vertically align text next to an image with CSS? (deals with case where image is larger, doesn't seem to apply here)

Understanding vertical-align

like image 712
Jeremy Kauffman Avatar asked Jun 15 '10 18:06

Jeremy Kauffman


People also ask

How do you center align a line vertically?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I align multiple images vertically?

Answer: Use the CSS vertical-align Property You can align an image vertically center inside a <div> by using the CSS vertical-align property in combination with the display: table-cell; on the containing div element.

How do I center an image inline?

An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it. To center an image using text-align: center; you must place the <img> inside of a block-level element such as a div .


2 Answers

The cause of your problem is that the image is vertically centered relative to the x-height of the surrounding text. This can be seen quite clearly on an enlarged screenshot where the baseline and mean line of the surrounding text is included:

illustration of a vertically aligned image compared to the surrounding text's baseline and mean line

The image is aligned similarly no matter which font size or line height you use. So, in a typographical sense, the image is actually correctly vertically aligned. However, if you'd like to adjust the alignment so that the center of the image is closer to the mean line, simply move it a bit upwards using margin-bottom:

img.icon {     margin-bottom: 0.25em; } 

The value of 0.25 em is rather arbitrarily chosen, based on what looked good when I tried it out. Adjust freely according to taste.

Here is a comparison before and after the margin-adjustment, with different font sizes.

like image 74
Pär Wieslander Avatar answered Oct 06 '22 00:10

Pär Wieslander


Why don't you set the image as background of the div element? I.e.:

<div style="line-height: 20px; background: url(path/image.gif) 5px 5px; no-repeat; padding-left: 40px;">   Blah blah blah </div> 

This will position the image on the top left. At least that's how I would do.

Regards

like image 35
Nik Chankov Avatar answered Oct 06 '22 01:10

Nik Chankov