Html 在 div 元素中垂直对齐文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6679966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:32:58  来源:igfitidea点击:

Vertically align text within div element

htmlcssvertical-alignment

提问by Tom

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.)

你知道这个交易,我有一个 div 元素,我需要在其中垂直对齐文本,但没有任何效果(位置:绝对;顶部:50%;边距顶部:-x;显示:表格单元格;垂直对齐:中间;等等,等等)

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).

这是我正在使用的(对不起内联 CSS)。无论如何,我会使用 line-height 但文本可以是一两行。它应该与图像垂直对齐,图像的最大高度始终为 30 像素(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>

采纳答案by Yi?it Yener

The idea is from hereand 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>

回答by brenjt

One other thing you can do. If it's only one line of text in the div you can use line-height

你可以做的另一件事。如果 div 中只有一行文本,您可以使用line-height

example

例子

div {
    line-height:40px;
}

回答by Ahsan Rathod

You need to do like this:

你需要这样做:

http://jsfiddle.net/rathoreahsan/5u9HY/

http://jsfiddle.net/rathoreahsan/5u9HY/

Use fixed heightinstead of paddingin main div. and use line height for left & right Divs

在主 div 中使用fixed height而不是padding。并为左右 Div 使用行高

回答by Yock

Here is a clean version of the solution

这是解决方案的干净版本

<div style="background: yellow">
<div style="width: 155px; font-size: 0.7em; height: 40px; display: table; overflow: hidden;">
    <div style="display: table-cell; vertical-align: middle;">
        <div style="">
            <a href="link">This is the text to vertically align</a>
        </div>
    </div>
</div>
<div style="clear: both"></div>

http://jsfiddle.net/5y4Nb/

http://jsfiddle.net/5y4Nb/

回答by ihimv

Is you need to show only a few lines of a very long text, here is the Fiddle. Adjust height according as needed.

您是否只需要显示几行很长的文本,这里是Fiddle。根据需要调整高度。

    .container-text {
    height:40px;
    width:180px;
    overflow-y:hidden;    
    display:table-cell;
    vertical-align:middle;
    text-align: center;
}

回答by Mattias Alfborger

Seems like a common float issue which can be fixed with a clearfixor, like i did in the following code snippet, with a fixed height of the wrapper.

似乎是一个常见的浮动问题,可以通过clearfix修复,或者像我在下面的代码片段中所做的那样,使用固定高度的包装器。

I also sat an line-heightof the floating divs and made it a little wider.

我还坐了一个line-height浮动的 div,让它更宽一些。

Take a look at this:

看看这个:

 <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;height:40px">  <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:185px;font-size:0.7em;height:40px;line-height:40px">  <a href="link">This is the text to vertically align</a>  </div>  </div> 

http://jsfiddle.net/YqxPZ/3/

http://jsfiddle.net/YqxPZ/3/