使用CSS在锚点内垂直对齐图像

Rob*_*ber 18 html css twitter-bootstrap

我想垂直对齐一个锚元素内的图像,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

我看了很多帖子,但没有一个对我有用.我也使用Bootstrap缩略图类,我不知道这是否相关,这是它不起作用的原因.

我已经读过我可以设置行高,但有些不对劲.

请看一个简单的plunker.

http://plnkr.co/edit/DsQ80oEiHFn4ma4qfNW8

更新: 我更新了plunker.文本垂直对齐工作正常,但它仍然不适用于图像.

Rah*_*zir 32

试试这个:

.thumbnail {
    line-height: 150px; // height taken from OPs plunker
}
.thumbnail img {
   margin: auto;
   vertical-align: middle; 
   display: inline-block;
} 
Run Code Online (Sandbox Code Playgroud)

添加display: inline-block;并设置line-height为父锚元素以垂直对齐中间图像.

现场例子

  • 锚标记将需要行高设置 (6认同)