html - Vertically align text next to an image?

ID : 402

viewed : 247

Tags : htmlcssalignmentvertical-alignmenthtml

Top 5 Answer for html - Vertically align text next to an image?

vote vote


Actually, in this case it's quite simple: apply the vertical align to the image. Since it's all in one line, it's really the image you want aligned, not the text.

<!-- moved "vertical-align:middle" style from span to img --> <div>   <img style="vertical-align:middle" src="">   <span style="">Works.</span> </div>

Tested in FF3.

Now you can use flexbox for this type of layout.

.box {    display: flex;    align-items:center; }
<div class="box">     <img src="">     <span style="">Works.</span> </div>

vote vote


Here are some simple techniques for vertical-align:

One-line vertical-align:middle

This one is easy: set the line-height of the text element to equal that of the container

<div>   <img style="width:30px; height:30px;">   <span style="line-height:30px;">Doesn't work.</span> </div> 

Multiple-lines vertical-align:bottom

Absolutely position an inner div relative to its container

<div style="position:relative;width:30px;height:60px;">   <div style="position:absolute;bottom:0">This is positioned on the bottom</div> </div> 

Multiple-lines vertical-align:middle

<div style="display:table;width:30px;height:60px;">   <div style="display:table-cell;height:30px;">This is positioned in the middle</div> </div> 

If you must support ancient versions of IE <= 7

In order to get this to work correctly across the board, you'll have to hack the CSS a bit. Luckily, there is an IE bug that works in our favor. Setting top:50% on the container and top:-50% on the inner div, you can achieve the same result. We can combine the two using another feature IE doesn't support: advanced CSS selectors.

<style type="text/css">   #container {     width: 30px;     height: 60px;     position: relative;   }   #wrapper > #container {     display: table;     position: static;   }   #container div {     position: absolute;     top: 50%;   }   #container div div {     position: relative;     top: -50%;   }   #container > div {     display: table-cell;     vertical-align: middle;     position: static;   } </style>  <div id="wrapper">   <div id="container">     <div><div><p>Works in everything!</p></div></div>   </div> </div> 

Variable container height vertical-align:middle

This solution requires a slightly more modern browser than the other solutions, as it makes use of the transform: translateY property. (

Applying the following 3 lines of CSS to an element will vertically centre it within its parent regardless of the height of the parent element:

position: relative; top: 50%; transform: translateY(-50%); 
vote vote


Change your div into a flex container:

div { display:flex; } 

Now there are two methods to center the alignments for all the content:

Method 1:

div { align-items:center; } 


Method 2:

div * { margin-top:auto; margin-bottom:auto; } 


Try different width and height values on the img and different font size values on the span and you'll see they always remain in the middle of the container.

vote vote


You have to apply vertical-align: middle to both elements to have it been centered perfectly.

<div>    <img style="vertical-align:middle" src="">    <span style="vertical-align:middle">Perfectly centered</span>  </div>

The accepted answer does center the icon around half of the x-height of the text next to it (as defined in the CSS specs). Which might be good enough but can look a little bit off, if the text has ascenders or descenders standing out just at top or bottom:

centered icon comparison

On the left, the text is not aligned, on the right it is as shown above. A live demo can be found in this article about vertical-align.

Has anyone talked about why vertical-align: top works in the scenario? The image in the question is probably taller than the text and thus defines the top edge of the line box. vertical-align: top on the span element then just positions it at the top of the line box.

The main difference in behavior between vertical-align: middle and top is that the first moves elements relative to the box's baseline (which is placed wherever needed to fulfill all vertical alignments and thus feels rather unpredictable) and the second relative to the outer bounds of the line box (which is more tangible).

vote vote


The technique used in the accepted answer works only for single-lined text (demo), but not multi-line text (demo) - as noted there.

If anyone needs to vertically center multi-lined text to an image, here are a few ways (Methods 1 and 2 inspired by this CSS-Tricks article)

Method #1: CSS tables (FIDDLE) (IE8+ (caniuse))


div {     display: table; } span {     vertical-align: middle;     display: table-cell; } 

Method #2: Pseudo element on container (FIDDLE) (IE8+)


div {    height: 200px; /* height of image */ }  div:before {   content: '';   display: inline-block;   height: 100%;   vertical-align: middle;   margin-right: -0.25em; /* Adjusts for spacing */ }  img {     position: absolute; }  span {   display: inline-block;   vertical-align: middle;   margin-left: 200px;  /* width of image */ } 

Method #3: Flexbox (FIDDLE) (caniuse)

CSS (The above fiddle contains vendor prefixes):

div {        display: flex;      align-items: center;     } img {     min-width: 200px; /* width of image */ } 

Top 3 video Explaining html - Vertically align text next to an image?