1

我需要你的帮助,

我似乎无法自动使 div 中的文本居中。

当前的: 在此处输入图像描述

预期结果: 在此处输入图像描述 这是 HTML 标记:

CSS

#alertBox_container {
    left: 50%;
    padding: 10px;
    top: 50%;
    margin: 0;
    padding: 0;
    height: 100%;
    border: 1px solid #808080;
    position: relative;
    color: rgb(11,63,113);
    background: #FFF;
    font-family: Arial;
}

#alertBox {
    height: 100px;
    width: 400px;
    bottom: 50%;
    right: 50%;
    position: absolute;
}

#alertBox_titlebar {
    line-height:24px;
    width: 100%;
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffffff", endColorstr="#cdcdcd");
    font-weight: bold;
    font-size: 9pt;
}

.alertBox_Button {
    color: #464646;
    border: 1px solid;
    border-color: #999 #666 #666 #999;
    background-color:#ccc;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#ffffff', EndColorStr='#E7E7E7');
}

.alertBox_Button:hover {
    background-color: #ddd;        
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#fafafa', EndColorStr='#dddddd');
    color: #000000;
}

#alertBox_close {
    line-height: 11px;
    width: 18px;

    font-size: 9pt;
    font-weight: bold;
    margin: 2px;
    padding: 1px;
    position:absolute;
    top:0px;
    right:0;
}

#alertBox_text_container {
    width: 100%;
    height: auto;
    text-align: center;
}
#alertBox_text {
    font-size: 9pt;
    width: 100%;
}
#alertBox_div_btn_OK {
    width: 100%;
    text-align: center;
    margin: 5px;
}

#alertBox_btn_OK {
    height: 20px;
    width: 50px;
    font-size: 9pt;
}

HTML

<div id="alertBox">
    <div id="alertBox_container">
        <div id="alertBox_titlebar"><span style="padding-left: 3px;">IMTS</span></div>
        <div><input class="alertBox_Button" id="alertBox_close" type="button" value="X" onclick="alertBox_hide()"></div>
        <div id="alertBox_text_container"><div id="alertBox_text">this is some text that should be vertically centered</div></div>
        </div>
    </div>
</div>

小提琴http://jsfiddle.net/VuEwu/

4

4 回答 4

2

这是一个解决方案:小提琴

主要思想是使用

display: table-cell
vertical-align: middle;

它可能不适用于不支持css display propertytable-cell值的旧浏览器。在这种情况下,一般来说,这里有一个非常好的解释和解决方案。

于 2013-09-25T18:55:36.730 回答
1

你可以使用解决它margin-top style

#alertBox_text_container {
    width: 100%;
    height: auto;
    text-align: center;
    margin-top:20px;
}

现场演示

于 2013-09-25T18:49:14.537 回答
1

垂直居中是棘手的。手动计算百分比并添加适当的边距或填充是最简单和最一致的。不过,还有其他方法可以做到这一点。我在下面提供了我所知道的四种方法。

css

.center-1{
    position:absolute;
    top:50%;
    width:100%;
    height:1em; /* need a fixed height for the element */
    margin-top:-.5em; /* offset height with negative margin */
    text-align:center;
}

.center-2{
    width:100%;
    height:100%;
    display: table-cell;/* IE8+ */
    vertical-align: middle;
    text-align:center;
}
.center-3{
    width:100%;
    line-height: 300px;/* height of your container */
    text-align:center;
}
.center-4{
    margin-top:29%;/* manual guess that will scale with the box */  
    width:100%;
    text-align:center;
}
.container{
    height:300px;
    width:500px;
    margin:20px;
    border:1px solid black;
    position:relative;
    display: table;/* needed for center-2 */
}

html

<div class="container">
    <div class="center-1">
        Center Me!
    </div>
</div>

<div class="container">
    <div class="center-2">
        Center Me!
    </div>
</div>

<div class="container">
    <div class="center-3">
        Center Me!
    </div>
</div>
<div class="container">
    <div class="center-4">
        Center Me!
    </div>
</div>

http://jsfiddle.net/xSML5/

于 2013-09-25T19:23:42.877 回答
0

我会取您想要将文本居中的区域的高度,然后将该数字添加到元素的行高。在这种情况下,您有 24px 的标题,为文本容器留下 76px。所以添加:

#alertBox_text_container {
    line-height: 76px;
}

应该得到你想要的。还有其他方法,但这是单线中心最简单的方法。

小提琴

于 2013-09-25T19:00:00.787 回答