簡體   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