是否有任何方法在主父元素中垂直或水平地居中html元素?


当前回答

你可以像这样直接写入你的CSS文件:

.content { 位置:绝对的; 上图:50%; 左:50%; 变换:翻译(-50%,-50%); } <div class = "content" > <p>一些文本</p> < / div >

其他回答

更新:虽然这个答案在2013年初可能是正确的,但现在不应该再用了。正确的解决方案使用偏移量,如下所示:

class="mx-auto"

至于其他用户的建议,也有本地引导类可用,如:

class="text-center"
class="pagination-centered"

对于水平居中,你可以这样做:

.centering{
float:none;
margin:0 auto
}

 <div class="span8 centering"></div>

我用这个

<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0 0;
    }

    .container-fluid {
        height: 100%;
        display: table;
        width: 100%;
        padding-right: 0;
        padding-left: 0;
    }

    .row-fluid {
        height: 100%;
        display: table-cell;
        vertical-align: middle;
        width: 100%;
    }

    .centering {
        float: none;
        margin: 0 auto;
    }
</style>
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="offset3 span6 centering">
                content here
            </div>
        </div>
    </div>
</body>

Bootstrap文档:

设置要显示的元素:块和居中通过边距。可作为mixin和类使用。

<div class="center-block">...</div>

我喜欢类似问题的答案:

在实际的表data <td>和表头<th>元素上使用bootstrap的文本中心类。所以 <td class="text-center">单元格数据</td> and <th class="text-center">头单元格数据</th>