在DIV容器内水平对齐文本

时间:2014-09-26 11:10:52

标签: html css html5 css3

我的结构如下:

    <div class="content-wrapper">
        <div class="left-wrapper">
            Lorem Ipsum... <!-- This is what I want horizontally aligned -->
        </div>
    </div>

我的CSS文件就像:

.content-wrapper {
    min-height: 200px;
    width: 100%;
}

.content-wrapper left-wrapper {
    min-height: inherit;
    width: 50%;
    float: left;
}

我想要做的是:left-wrapper中的文字有多大,我想要padding-toppadding-bottom等于30px例如,我的文字完全水平对齐。

提前致谢!

4 个答案:

答案 0 :(得分:2)

CSS:

.content-wrapper .left-wrapper {
    min-height: inherit;
    width: 50%;
    float: left;
    text-align:center;
    padding:30px 0;
}

这是JSBIN.

答案 1 :(得分:0)

.content-wrapper .left-wrapper {
    min-height: inherit;
    width: 50%;
    float: left;
    text-align: center;
}

您可以在此处查看:http://jsfiddle.net/0dfxv8tw/

答案 2 :(得分:0)

在您的代码中替换

.content-wrapper left-wrapper {

.content-wrapper .left-wrapper {

并提供

text-align: center;

答案 3 :(得分:0)

HTML

<div class="div01">
<div class="div02">
    <div class="div03">
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </div>
</div>

CSS

*{margin:0; padding:0; font-size:12px; color:#333; font-family:Arial;}
.div01 {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
}
.div02{
height:100%;
width:100%;
display:table;
}
.div03 {
vertical-align:middle;
height:100%;
display:table-cell;
border:1px solid #000;
line-height:22px;
}

Fiddle