在 div 元素内垂直对齐文本
我知道这个问题已经被问死了,但通过搜索没有任何结果对我有用。
你知道这个交易,我有一个 div 元素,我需要垂直对齐文本,但没有任何效果 (position:absolute;top:50%;margin-top:-x;display:table-cell;vertical-align:middle ;等等,等等)
这是我正在处理的内容(抱歉内联CSS)。无论如何,我会使用行高,但文本可以是一两行。它应该与最大高度始终为 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>
I know this question has been asked to death but nothing through searching has worked for me.
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.)
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您还可以做另一件事。如果 div 中只有一行文本,您可以使用
line-height
示例
One other thing you can do. If it's only one line of text in the div you can use
line-height
example
这个想法来自此处,应该适用于所有浏览器。
The idea is from here and should work for all browsers.
您需要这样做:
http://jsfiddle.net/rathoreahsan/5u9HY/
使用
固定高度
而不是主div中的填充
。并使用左侧和右侧的行高右分区You need to do like this:
http://jsfiddle.net/rathoreahsan/5u9HY/
Use
fixed height
instead ofpadding
in main div. and use line height for left & right Divs这是解决方案的干净版本
http://jsfiddle.net/5y4Nb/
Here is a clean version of the solution
http://jsfiddle.net/5y4Nb/
似乎是一个常见的浮动问题,可以使用 clearfix 或类似的方法来解决我在下面的代码片段中做了,包装器的高度固定。
我还设置了浮动 div 的
line-height
并使其更宽一些。看看这个:
http://jsfiddle.net/YqxPZ/3/
Seems like a common float issue which can be fixed with a clearfix or, like i did in the following code snippet, with a fixed height of the wrapper.
I also sat an
line-height
of the floating divs and made it a little wider.Take a look at this:
http://jsfiddle.net/YqxPZ/3/
如果您只需要显示很长文本的几行,这里是 Fiddle。根据需要调整高度。
Is you need to show only a few lines of a very long text, here is the Fiddle. Adjust height according as needed.