简体   繁体   English

如何在引导程序中将div垂直居中?

[英]How can I center vertically a div in bootstrap?

I have a following html code: 我有以下html代码:

<div class="text-center">
 <div class="row">
    <div class="col-md-6" id="centeredVertically">

        <div class="col-md-12">gdsgdsgdsgdshf</div> 
        <div class="col-md-12">gdsgdsgdsgdshf</div> 
        <div class="col-md-12">gdsgdsgdsgdshf</div> 
        <div class="col-md-12">gdsgdsgdsgdshf</div> 
        <div class="col-md-12">gdsgdsgdsgdshf</div> 
        <div class="col-md-12">gdsgdsgdsgdshf</div> 


    </div>
    <div class="col-md-6"><img src="http://cg.cs.uni-bonn.de/aigaion2root/attachments/poplar-leaves.png-fd1e71ded8cdc260ea04806113869bc3.png" alt="tree"></div>
</div>
    <h4>header.</h4>
    <p>hey there</p>
    <div class="paddown">

        <a class="btn btn-default" name="close" id="close" >Close</a>
    </div>
</div>

and when you resize the webpage wide enough - there are two divs next to each other. 当您将网页调整为足够大的尺寸时-两个div彼此相邻。 I want the div with the text centeredVertically to be vertically centered on the left side of the tree's picture. 我希望带有centeredVertically文本的div在树的图片的左侧垂直居中。 I tried following one of top answers from other stack overflow posts and I added this css class to my divs: 我尝试遵循其他堆栈溢出帖子的最佳答案之一,并将此css类添加到div中:

.vcenter {
    display: inline-block;
    vertical-align: middle;
    float: none;
}

but it didn't do the trick. 但这并不能解决问题。 How can I fix it? 我该如何解决?

My fiddle is here http://jsfiddle.net/2wujw71x/12/ 我的小提琴在这里http://jsfiddle.net/2wujw71x/12/

Thanks. 谢谢。

You can use something like this (using tables): 您可以使用如下形式(使用表格):

HTML: HTML:

<div class="text-center">

  <div class="row row-same-height">

    <div class="col-md-6 col-md-height col-middle">

      <div>gdsgdsgdsgdshf</div> 
      <div>gdsgdsgdsgdshf</div> 
      <div>gdsgdsgdsgdshf</div> 
      <div>gdsgdsgdsgdshf</div> 
      <div>gdsgdsgdsgdshf</div> 
      <div>gdsgdsgdsgdshf</div> 

    </div>

    <div class="col-md-6  col-md-height col-middle">
      <img src="http://cg.cs.uni-bonn.de/aigaion2root/attachments/poplar-leaves.png-fd1e71ded8cdc260ea04806113869bc3.png" alt="cat">         
     </div>

  </div>

  <h4>Header</h4>

  <p>Hey there</p>

  <div class="paddown">
    <a class="btn btn-default" name="close" id="close" >Close</a>
  </div>

</div>

CSS: CSS:

.row-same-height {
  display: table;
  width: 100%;
}
.row-same-height.img-responsive {
  width: 100% !important;
  max-width: 100% !important;
}
.col-xs-height {
  display: table-cell;
  float: none !important;
}
@media (min-width: 768px) {
  .col-sm-height {
    display: table-cell;
    float: none !important;
  }
}
@media (min-width: 992px) {
  .col-md-height {
    display: table-cell;
    float: none !important;
  }
}
@media (min-width: 1200px) {
  .col-lg-height {
    display: table-cell;
    float: none !important;
  }
}
.col-top {
  vertical-align: top;
}
.col-middle {
  vertical-align: middle;
}
.col-bottom {
  vertical-align: bottom;
}

FIDDLE 小提琴

This CSS allows you to have columns with the same height and you can position them in the middle. 该CSS允许您具有相同高度的列,并且可以将它们放置在中间。 This works even on IE8. 即使在IE8上也可以使用。

You can read more about this on this page . 您可以在此页面上阅读有关此内容的更多信息。

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

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