Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align image inside an anchor with CSS

Tags:

I would like to vertical align an image inside an anchor element like this:

<ul class="thumbnails">     <li class="span2">          <a href="#" class="thumbnail">            <img src="http://www.forodefotos.com/attachments/barcos/17639d1298388532-barcos-vapor-antiguos-barcos-antiguos-vapor-mercantes.jpg" style="max-height: 150px">         </a>     </li> </ul> 

I have read many posts but none of them works for me. I´m also using a Bootstrap thumbnails class, I don´t know if this is related and it´s the reason it doesn't work.

I have read I can do it setting a line-height but something is wrong.

Please see a simple plunker.

http://plnkr.co/edit/DsQ80oEiHFn4ma4qfNW8

UPDATE: I have updated the plunker. Text vertical align is working ok, but it still doesn´t work for images.

like image 307
Rober Avatar asked Dec 20 '13 09:12

Rober


People also ask

How do I align text and image in anchor tag?

Add display: inline-block; and set the line-height to the parent anchor element to middle align images vertically.

Can I put image inside anchor tag?

Solution 1. Of course you can put an IMG tag inside an A tag. There's nothing wrong with it. As for "apply styling to the image itself", that all depends on what you want to do.

How do I align vertically in CSS?

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


1 Answers

Try this:

.thumbnail {     line-height: 150px; // height taken from OPs plunker } .thumbnail img {    margin: auto;    vertical-align: middle;     display: inline-block; }  

Add display: inline-block; and set the line-height to the parent anchor element to middle align images vertically.

LIVE EXAMPLE

like image 183
Rahil Wazir Avatar answered Sep 21 '22 13:09

Rahil Wazir