Bootstrap - 将DIV与顶部,中间和底部对齐

Ale*_*lex 5 html css twitter-bootstrap twitter-bootstrap-3

我需要在容器DIV中有三个DIV,所有DIV都水平居中.第一个需要与容器的垂直顶部对齐,第二个与垂直中心对齐,第三个与垂直底部对齐.这是垂直定位div 的答案,但不解决其他项目.这里有另一个答案,但是你如何添加需要垂直对齐顶部和底部的DIV?

这是HTML:

<div class="carousel-caption"> <!-- outer container; all items horizontally centered -->
    <div class="row align-top"> <!-- align this DIV to top -->
        <h1 class="col-sm-12">Top DIV</h1>
    </div>
    <div class="row align-vertical-center"> <!-- align this DIV to center -->
        <div class="col-sm-12 ">Middle DIV</div>
    </div>
    <div class="row align-vertical-bottom">
        <div class="align-vertical-bottom">Bottom DIV</div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Ted*_*Ted 9

对于这个HTML:

<div class="container">
  <div class="carousel-caption"> <!-- outer container; all items horizontally centered -->
    <div class="row vtop"> <!-- align this DIV to top -->
        <div class="col-sm-12">Top DIV</div>
    </div>
    <div class="row vcenter"> <!-- align this DIV to center -->
        <div class="col-sm-12 ">Middle DIV</div>
    </div>
    <div class="row vbottom">
        <div class="col-sm-12 vbottom">Bottom DIV</div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这个CSS:

.carousel-caption{
    padding:0;
 }

.vtop{
  /*padding on parent fixes this*/
}

.vcenter{
    position: relative;
    top: 50%;
    transform: translateY(-50%); 
}

.vbottom{
    position: relative;
    top: 100%;
    transform: translateY(-100%); 
}
Run Code Online (Sandbox Code Playgroud)

请参阅此Bootply演示

HTH!

  • 前50%将中间div的顶部置于容器高度的50%处,然后translateY(-50%)将其向上移动div的高度的50%,从而将其直接定位在中间.底部div的顶部位于容器的最底部,因此translateY(-100%)将其向上移动div的高度的100%,因此它恰好位于底部.好的解决方案 (2认同)