Bootstrap 3容器垂直对齐中心与页眉和页脚

时间:2017-09-05 13:57:43

标签: css twitter-bootstrap

我有代码,包含标题,容器(内容)和页脚,我无法将容器垂直对齐,我可以这样做:

html, body {
  height: 100%;
}

footer {
  height: 50px;
  background: #eee;
  text-align: center;
  line-height: 50px;
  position: absolute;
  width: 100%;
  bottom: 0;
}

.text {
  padding-bottom: 20px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
  
  <div id="app">
    
    <nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
    
  </div>  
  
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="col-md-6 text">
          <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum dicta magni neque saepe sit ducimus culpa laudantium corporis doloribus delectus mollitia similique blanditiis ratione et, magnam. Expedita blanditiis in sunt.
          </p>
        </div>
        <div class="col-md-6 text">
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis consequatur, voluptatem harum excepturi, earum similique dolorem nisi deserunt id ipsam vitae explicabo, nostrum. Accusantium quos aut ad, adipisci quod optio.
          </p>
        </div>
        <div class="col-md-6 text">
          <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum dicta magni neque saepe sit ducimus culpa laudantium corporis doloribus delectus mollitia similique blanditiis ratione et, magnam. Expedita blanditiis in sunt.
          </p>
        </div>
        <div class="col-md-6 text">
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis consequatur, voluptatem harum excepturi, earum similique dolorem nisi deserunt id ipsam vitae explicabo, nostrum. Accusantium quos aut ad, adipisci quod optio.
          </p>
        </div>
      </div>
    </div>
  </div>
  
  
  <footer>
    
    text center;
    
  </footer>
 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
</body>
</html>

如何考虑高度页眉和页脚与绝对位置垂直对齐中心容器;?请帮我。我有4列col-md-6。

示例:http://jsbin.com/miboyuqewu/edit?html,css,output

2 个答案:

答案 0 :(得分:0)

使用FlexBox

.container {
  display: flex;
  align-items: center;
  height: calc(100% - $navHeight - $footerHeight);
}

但我建议您为容器添加父div并定位.body-content

<div class="body-content">
  <div class="container">
    @content
  </div>
</div>

答案 1 :(得分:0)

您可以使用CSS3 transform翻译内容的位置。

执行以下操作:

  1. 将包装类添加到内容容器中。
  2. position: relative;添加到正文的CSS。
  3. 对于新的包装类,添加以下CSS:

    .wrapper {
      position: absolute;
      top: 50%;
      -webkit-transform: translateY(-50%);
      -ms-transform: translateY(-50%);
      transform: translateY(-50%);
    }