繁体   English   中英

如何使用Bootstrap垂直和水平居中一个块?

[英]How I can center a block vertically and horizontally with Bootstrap?

我有这样的形式:

<div class="container-fluid">
<div class="span5 well">
<h2>Authentification</h2>
<form method="POST">...</form>
</div>
</div>

我怎样才能将该块(div span5井)垂直和水平居中? 谢谢。

可以通过向没有兄弟节点的元素添加margin: 0 auto来完成水平居中。

<style>
.well
{       
    position:absolute;      
    width:200px;    
    height:200px;
    top: 50%;
    left: 50%;
    margin-top:-100px; /* negative number 1/2 height */
    margin-left:-100px; /* negative number 1/2 width */
    outline:1px solid #CCC;
}
</style>

<div class="container-fluid">
<div class="span5 well">
<h2>Authentification</h2>
<form method="POST">...</form>
</div>
</div>

与Bootstrap没有任何冲突,可以使用一些JS,比如这个人的JQuery.vAlign插件 我发现它比垂直对齐技巧更直接。

前面已提到水平居中设置边距0自动。

要动态地垂直对齐元素(当您不知道高度时),可以相对移动50%(向上或向下),然后使用transform:translateY(50%)(+/-)将其置于其父容器中心:

div.span5 {
    margin: 0 auto;
    top:50%;
    width:400px;
    position:relative;
    transform: translateY(-50%);
}

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM