简体   繁体   English

将表格单元格中的文本垂直对齐,图像浮动到左侧?

[英]Vertically align text in table cell with image floated to the left?

The html: 的HTML:

<td>
    <img>
    text here
</td>

The css: CSS:

td img{
    display: block;
    float: left;
} 

I want the picture to be floated to the left within the cell, and the text to be vertically-aligned to the middle. 我希望图片在单元格中向左浮动,文本在中间垂直对齐。 Without the picture there, the text is automatically vertically aligned to the middle, but with the picture there I can't seem to change the vertical alignment of the text. 没有图片,文字会自动垂直对齐到中间,但是有了图片,我似乎无法更改文字的垂直对齐方式。

Any ideas? 有任何想法吗?

If you know the height of the image itself you can use the line-height property. 如果您知道图像本身的高度,则可以使用line-height属性。

<td style="line-height: 50px;">
    <img>Text text text
</td>

This should force the text to be displayed in the center of the line-height. 这应强制将文本显示在行高的中心。

Try setting vertical-align:middle; 尝试设置vertical-align:middle; in the CSS for the img . 在CSS中的img You may also want to consider setting that image as a background to that table cell, as you may have cross-browser issues regardless of how you position everything (setting the image as a background would avoid this). 您可能还需要考虑将该图像设置为该表格单元的背景,因为无论如何放置所有内容,您都可能会遇到跨浏览器的问题(将图像设置为背景可以避免这种情况)。

Using line-height to vertically align text next to an image within a table cell onl* y works if you have one line of text. 如果一行 只有一行,则仅使用行高在表格单元格中的图像旁边垂直对齐文本即可 The next line of text will be (as in the example above) 50px below the first line of text. 下一行文字(如上例所示)将在第一行文字下方50px。 * *

Setting the * image as backgroun *d also does not work unless you set a margin within the cell = to the width of the image on whichever side you want the image to align. 将* 图像设置为背景 * d也不起作用,除非您将单元格=中的边距设置为要对齐图像的一侧的图像宽度。

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

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