简体   繁体   中英

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

I have this form:

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

How I can center that block (div span5 well) vertically and horizontally? Thank you.

可以通过向没有兄弟节点的元素添加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>

There's nothing conflicting with Bootstrap to use some JS for this, such as this guy's JQuery.vAlign plugin . I find that more straight-forward than the vertical-align tricks.

Horizontal centering was mentioned earlier to set margin 0 auto.

To vertically align the element dynamically (when you don't know the height) you can relatively shift it 50% (up or down) and then use transform: translateY(50%) (+/-) to center it within its parent container:

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

fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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