垂直居中对齐文本

时间:2011-02-10 03:01:53

标签: html css

好的,这就是HTML元素现在的样子:

enter image description here

我希望文本以图像为中心。图像为32x32px。

这是我的代码:

<style>
div.interface {
    line-height: 32px;
    float: left;
    margin: 10px;
    width: 450px;
    font: 14px/27px 'CalibriRegular', Arial, sans-serif;
    color: #646464; text-shadow: 1px 1px 1px #fff;
    padding-right: 10px;
}
</style>

<div class="interface"><img src="assets/icons/interface.png" />Filled with fun animations and eye-candy!  Complete with Retina graphics.</div>

2 个答案:

答案 0 :(得分:32)

要使文本居中,请将行高设置为与高度相同,例如:

img{ height: 30px; }
#text{ line-height: 30px; }

并将vertical-alignment设置为middle:

#text{ line-height: 30px; vertical-align:middle; }

答案 1 :(得分:12)

style="vertical-align: middle"添加到您的<img />代码。

有关vertical-align标记的完整说明,请参阅http://phrogz.net/CSS/vertical-align/index.html