问题描述
我知道这个问题已被要求死亡,但通过搜索没有任何效果对我有用。
I know this question has been asked to death but nothing through searching has worked for me.
你知道交易,我有一个div元素,我需要垂直对齐文本但没有什么工作(位置:绝对;顶部:50%; margin-top:-x; display:table-cell; vertical-align:middle; etc。)
You know the deal, I have a div element that I need to vertically align text in but nothing has worked (position:absolute;top:50%;margin-top:-x;display:table-cell;vertical-align:middle;etc., etc.)
这是我正在使用的(对于内联CSS的遗憾)。无论如何,我会使用行高,但文本可以是一行或两行。它应该与始终为30像素(30x50)的最大高度的图片垂直对齐。
Here is what I am working with (sorry for the inline CSS). Anyway, the I would use line-height but the text can be one or two lines. It should vertical align with the image which is always max-height of 30px (30x50).
<div style="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;">
<div style="float:left;width:55px;height:40px;">
<a href="link"><img style="max-width:50px;border:1px solid #ffb92c;" src="image.jpg" alt="" /></a>
</div>
<div style="float:right;width:155px;font-size:0.7em;height:40px;">
<a href="link">This is the text to vertically align</a>
</div>
</div>
推荐答案
想法来自,并且应适用于所有浏览器。
The idea is from here and should work for all browsers.
<div style="margin: 0 0 10px 0; padding: 10px; border: 2px solid #606060; background-color: #2b2b2b;
-webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px;">
<div style="float: left; width: 55px; height: 40px;">
<a href="link">
<img style="max-width: 50px; border: 1px solid #ffb92c;" src="image.jpg"
alt="" /></a>
</div>
<div style="float: right; width: 155px; font-size: 0.7em; height: 40px; display: table; #position: relative; overflow: hidden;">
<div style="#position: absolute; #top: 50%; display: table-cell; vertical-align: middle;">
<div style="#position: relative; #top: -50%;">
<a href="link">This is the text to vertically align</a>
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
这篇关于在div元素中垂直对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!