简体   繁体   English

在图像的中间对齐文本?

[英]Aligning a text in middle next to an image?

I have a table column like this, where I want to align an image and a text. 我有一个这样的表格列,我想在其中对齐图像和文本。 The text must be in the middle of the image. 文本必须在图像的中间。

<td><img height='50px' src='blabla'>Hello</td>

The above aligns the text at the bottom of the image. 上面的内容使图像底部的文本对齐。

I have also tried adding vertical-align:middle to the td style property, without luck. 我也尝试过在td样式属性vertical-align:middle添加vertical-align:middle ,但不走运。

I have also tried adding a <font> and styling that to vertical-align:middle without luck. 我也尝试过添加<font>并将其样式设置为vertical-align:middle没有运气。

I know of one way, and that is adding another table column and having the text and image in separate columns, then it would be possible to align the text vertically in the middle, but I am trying to avoid this. 我知道一种方法,那就是添加另一个表列,并在单独的列中包含文本和图像,然后可以在中间垂直对齐文本,但是我试图避免这种情况。

Any ideas? 有任何想法吗?

You have to add the vertical-align to the img, not the td : 您必须将vertical-align添加到img,而不是td

<td>
  <img style="vertical-align: middle;" src="test.jpg"/>
  test
</td>

To apply the style to all images in td s, use an external CSS: 要将样式应用于td的所有图像,请使用外部CSS:

td img {
  vertical-align: middle;
}

Have you tried using valign="middle" 您是否尝试过使用valign =“ middle”

<td valign="middle"><img height='50px' src='blabla'>Hello</td>

Also, no need to add "px" to the height of the img. 另外,无需在img的高度添加“ px”。

Here you go: 干得好:

<table>
<tr>
<td><img height='50' width='50' src='http://www.google.com/images/logos/ps_logo2.png' style='vertical-align:middle;">Hello</td>
</tr>
</table>

http://jsfiddle.net/FDRy8/ http://jsfiddle.net/FDRy8/

use vertical-align:middle on the image and not the td 在图像上使用vertical-align:middle而不是td

td img{
vertical-align:middle;
}

working example at http://www.jsfiddle.net/gaby/pPVtH/ http://www.jsfiddle.net/gaby/pPVtH/上的工作示例

也向行添加垂直对齐,而不仅仅是单元格。

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

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