简体   繁体   English

如何使用bootstrap垂直居中对齐div?

[英]How to center align a div vertically which uses bootstrap?

I have a row with content which should be in the center of the site and a footer at the bottom. 我有一行内容应位于网站的中心,底部是一个页脚。 The content are divided into two 6 column . 内容分为两6 column i want it to center align vertically according to the screen size. 我希望它根据屏幕尺寸垂直居中对齐。 so i tried to give some percentage of top with a position relative but it doesn't work out. 所以我试图给位置relativetop一些百分比,但它没有成功。

If i give margin-top it is working. 如果我给予margin-top它正在工作。 In order to make the top we have to give the position, so i have given it relative I don't want position absolute then the footer will come at the top of the site. 为了使顶部我们必须给出位置,所以我给它relative我不希望位置absolute然后页脚将来到站点的顶部。 Other content also will come top i thnik. 其他内容也将成为我的首选。

How to center align the contents and footer should stay at bottom all time all display size. 如何对齐内容和页脚应始终保持在底部所有显示大小。

HTML HTML

<div class='row'>
<div class='col-md-6 vertical'> content</div>
<div class='col-md-6 vertical'>content right</div>
</div>
<footer>sit at bottom</footer>

CSS CSS

.vertical{
    position:relative;
   /* margin-top:20%; */ /*works*/
    top:20%; /*not works even providing position */
}

JSFIDDLE NOTE: Don't forgot to resize the window then only you can able to see the two column division JSFIDDLE 注意:不要忘记调整窗口大小,只有你能看到两列分割

<div class='row'>
 <div class='col-md-6 '>
   <div class="vertical"> content</div>
 </div>
 <div class='col-md-6 '>
   <div class="vertical"> content</div>
 </div>
</div>
<footer>sit at bottom</footer>

.vertical{
  display: table-cell;
  vertical-align:middle;
  height: 200px /* Accordingly */
}

Working Fiddle Fiddle . 工作小提琴小提琴

Apart from using display: table-cell , there is one more way to do it but is not so cross-browser at this moment. 除了使用display: table-cell ,还有一种方法可以做到这一点,但此时并不是那么跨浏览器。 Take a look at CSS3 Flexbox . 看看CSS3 Flexbox

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

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