垂直对齐跨越两行的元素的文本时出现问题

时间:2011-05-30 19:25:50

标签: html css xhtml

我想使用CSS在元素中间对齐一些文本。这是我的标记:

<div id="testimonial">
    <span class="quote">Some random text that spans two lines</span>
</div>

相关的CSS:

#testimonial {
    background: url('images/testimonial.png') no-repeat;
    width: 898px;
    height: 138px;
    margin: 0 auto;
    margin-top: 10px;
    text-align: center;
    padding: 0px 30px 0px 30px;
}

.quote {
    font-size: 32px;
    font-family: "Times New Roman", Verdanna, Arial, sans-serif;
    vertical-align: middle;
    font-style: italic;
    color: #676767;
    text-shadow: 1px 1px #e7e7e7;
}

通常要在.quote的垂直中间获取#testimonial,我会这样做:

.quote { line-height: 138px; }

但这会打破布局,因为.quote中的文字跨越多行。

正如您所看到的,我已经尝试过vertical-align: middle;但这也无效。

感谢任何帮助。欢呼声。

5 个答案:

答案 0 :(得分:16)

recently found outvertical-align: middle;line-height: 0;结合后,具有未定义尺寸的内容的垂直居中非常好。

查看此demonstration fiddle

HTML:

<div id="testimonial">
    <span><span class="quote">Some random text<br />that spans two lines</span></span>
</div>

CSS:

#testimonial {
    background: #333 url('images/testimonial.png') no-repeat;
    width: 898px;
    height: 138px;
    margin: 0 auto;
    margin-top: 10px;
    text-align: center;
    padding: 0 30px 0 30px;
    line-height: 138px;
}
#testimonial>span {
    display: inline-block;
    line-height: 0;
    vertical-align: middle;
}
.quote {
    font-size: 32px;
    font-family: "Times New Roman", Verdanna, Arial, sans-serif;
    font-style: italic;
    color: #676767;
    text-shadow: 1px 1px #e7e7e7;
    line-height: 32px;
}

答案 1 :(得分:3)

您可以使用单跨度更简单地完成此操作 http://jsfiddle.net/7ebLd/

#testimonial {
    height: 138px;
    line-height: 138px;
}
span {
    display: inline-block;
    line-height: 19px;
    vertical-align: middle;
}

答案 2 :(得分:2)

因为没有人用表格单元解决方案回答它。

在这里 - 你也可以使用IE6 / 7解决方案(虽然它涉及到一个yukky HTML hack),正如@thirtydot在评论中所说,IE7及以下版本不支持表格显示属性 -

如果你不想/喜欢额外的HTML元素,你可以给IE7及以下.quote上的一些顶部填充 - 这样虽然它不能准确地垂直居中,但它可能是可以接受的退后

<强> CSS:

#testimonial {
    background: #eee url('images/testimonial.png') no-repeat;
    width: 898px;
    height: 138px;
    margin: 10px auto 0;
    padding: 0 30px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

.quote {
    font-size: 32px;
    font-family: "Times New Roman", Verdana, Arial, sans-serif;
    font-style: italic;
    color: #676767;
    text-shadow: 1px 1px #e7e7e7;
}

IE CSS:

<!--[if lte IE 7]>
<style type="text/css" media="screen">
#testimonial i {
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

.quote {
    display: inline-block;
    width: 100%;
    vertical-align: middle;
}
</style>
<![endif]-->

<强> HTML:

<div id="testimonial">
    <i></i>
    <span class="quote">Some random text <br> that spans two lines</span>
</div>

答案 3 :(得分:1)

本网站提供了大量的options for vertical centering with css

如果您可以在.quote上设置高度,我认为方法2可以在这种情况下工作:

.quote {
     position:absolute;
     top:50%;
     height:240px;
     margin-top:-120px; /* negative half of the height */
}

另一种选择是在CSS中使用display:table-cell; vertical-align:middle;方法,但此选项在IE中不起作用,因此您还必须设置特定于IE的版本。

答案 4 :(得分:0)

本网站:

http://www.emblematiq.com/blog/vertical_align_with_css/

结果:

http://www.emblematiq.com/blog/vertical_align_with_css/assets/03.html

使用以下标记/ css使用display: table-cellvertically-align: middle方法垂直居中多行:

<div id="wrapper">
    <img src="0.gif" alt="stuff" id="fixed" />
    <div id="floating"><div><div>
<p>Middle aligned text goes right here and it does wrap nicely at the end of the line</p>
    </div></div></div>
</div>
p {
    margin:0;
    padding:0;
}
#wrapper {
    width:550px;
    border:1px solid #666;
    padding:10px;
    height:300px;
}
#fixed {
    float:right;
    width:200px;
    height:300px;
    background:#666;
    display:block;
}
#wrapper>#floating { /*display:table for Mozilla & Opera*/
    display:table;
    position:static;
}
#floating { /*for IE*/
    width:300px;
    height:100%;
    background:#EAEAEA;
    position:relative;
}
#floating div { /*for IE*/
    position:absolute;
    top:50%;
}
#floating>div { /*for Mozilla and Opera*/
    display:table-cell;
    vertical-align:middle;
    position:static;
}
#floating div div {
    position:relative;
    top:-50%;
}

Result