简体   繁体   English

垂直顶部对齐文本

[英]Vertically top align the text

Here is the DEMO which I have tried so far. 这是到目前为止我尝试过的DEMO

What I need is to push the text to the exact top of the div so that it matches the image top alignment. 我需要将文本推到div的确切顶部,以使其与图像顶部对齐。

I want Both image and the text to be align exactly like in one line, now you can see the little space on the top of the text which is not perfectly aligned to the top. 我希望图像和文本都完全像在一行中一样对齐,现在您可以看到文本顶部的小空间没有完全对齐到顶部。

HTML 的HTML

<div class="wrap"> 
    <div class="image"><img src="" /></div>
    <div class="content">Lorem ipsum</div>
</div>

CSS 的CSS

.wrap{background:grey}
.image{display:table-cell; vertical-align:top}
.content{display:table-cell; vertical-align:top; font-size:24px}

http://jsfiddle.net/s38Uv/20/ http://jsfiddle.net/s38Uv/20/

You don't need display:table-cell to vertically align to top - that is default behaviour. 不需要 display:table-cell垂直对齐顶部-这是默认的行为。

The reason you have a gap is that the line-height isn't exactly the same as the virtual box around the text. 出现间隙的原因是行高与文本周围的虚拟框不完全相同。 The difference depends on the font itself. 区别取决于字体本身。 If you are working for explorer 7 and above, you can set the text to display:inline-block , then margin-top: -0.Xem where X is an arbitrary number. 如果使用的是Explorer 7及更高版本,则可以将文本设置为display:inline-block ,然后设置margin-top: -0.Xem ,其中X是任意数字。 You can set margin-top with pixels too, of course. 当然,您也可以设置带有像素的页边空白。

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

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