简体   繁体   English

如何在表格单元格中垂直对齐文本和浮动图像?

[英]How do I vertically align text and a floated image in a table cell?

How can I vertically center text on 1 line ? 如何text on 1 line垂直居中text on 1 line The text may change, so I want a generic solution. 文本可能会更改,所以我需要一个通用的解决方案。

Example here: http://jsfiddle.net/rfECj/ 此处的示例: http : //jsfiddle.net/rfECj/

HTML: HTML:

<table>
    <tr>
        <td>
            <img src="img.png"/>
            text on 2 lines text on 2 lines text on 2 lines
        </td>
        <td>
            <img src="img.png"/>
            text on 2 lines text on 2 lines text on 2 lines
        </td>
        <td>
            <img src="img.png"/>
            text on 1 line
        </td>
    </tr>
</table>

CSS: CSS:

table img {
    margin: 4px 6px 0 6px;
    float: left;
}

table tr td {
    width: 180px;
}

The only solution that comes in mind is use more TDs, one for the image, one for the text. 想到的唯一解决方案是使用更多的TD,一个用于图像,一个用于文本。

http://jsfiddle.net/rfECj/5/ http://jsfiddle.net/rfECj/5/

anything wrong with using vertical-align: middle; 使用vertical-align: middle;有任何问题vertical-align: middle; ?

Hard to tell if this is what you want from the question, but you could use vertical-align , which works on td s. 很难确定这是否是您要从问题中得到的,但是您可以使用vertical-align ,它可以在td

table tr td {
    width: 180px;
    border:1px solid red; //just to see it better
    vertical-align:middle;   // add this
}

http://jsfiddle.net/rfECj/9/ http://jsfiddle.net/rfECj/9/

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

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