简体   繁体   English

如何在HTML中使文本与图像中心垂直对齐?

[英]How to vertical align text with center of image in HTML?

So I have an image "blah.jpg" and a name "Name" in HTML. 因此,我在HTML中有一个图片“ blah.jpg”和一个名称“ Name”。

    <img src="blah.jpg"> Name

I want to make it so that the Name is center aligned with the center of the blah.jpg image instead of the text "Name" having a baseline at the bottom of the image. 我要使其名称与blah.jpg图像的中心居中对齐,而不是在图像底部具有基线的文本“名称”。

What is the best way to accomplish this with no tables using CSS3 if possible? 如果可能的话,没有表格的最佳方法是什么?

In the page: 在页面中:

<img src="blah.jpg" class="centered"> Name

In css file: 在css文件中:

img.centered {vertical-align:middle;}

No need for display: inline-block (which is not supported in some old browsers) 无需显示:内联代码块(某些旧版浏览器不支持)

您可以在img元素上使用vertical-align:middle: http : //jsfiddle.net/Z35pw/

如果您想要单行而不定义额外的对象或属性,只需在<img .../>标记内添加style="vertical-align:middle"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM