使用中心块进行Bootstrap垂直对齐

时间:2016-10-14 08:53:55

标签: html css twitter-bootstrap-3

我用bootstrap类属性center-block创建了div,我想垂直对齐但是没有任何工作只能手动为div添加margin。任何提示如何实现这一目标? Css代码:

body {
  margin-bottom: 31px;
  font-family: 'Lato', sans-serif;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 31px;
  border-top: 1px solid white;
}
.menu{
  width: 642px;
  height: 642px;
  border: 1px solid white;

}
.menu > p{
  width: 300px;
  height: 300px;
  margin: 10px;
  float: left;
  color: white;
  text-align: center;
  font-size: 28px;
  text-shadow: 2px 2px 5px black;
}


Html代码:

     <!DOCTYPE html>
    <html lang="en">
    <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
      <style>
      body{
        background-image: url(img/landscape1.jpg);
        background-size: cover;
      }
      </style>
      <title>Website</title>
    </head>
    <body>
      <div class="container-fluid">
        <div class="page-header">
          <h1 style="color: white;">Logo</h1>
        </div>
        <div class="center-block menu">
          <p id="">demo1</p>
          <p id="">demo2</p>
          <p id="">demo3</p>
          <p id="">demo4</p>
        </div>
      </div>
        <footer class="footer">
            <div class="container col-lg-6 col-md-6 col-sm-6 col-xs-6">
            <p class="text-muted">Company all rights reserved &copy; </p>
          </div>
        </footer>





    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

body {
  margin-bottom: 31px;
  font-family: 'Lato', sans-serif;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 31px;
  border-top: 1px solid white;
}
.menu{
  width: 640px;
  height: 640px;
}
.menu li > p{
  width: 200px;
  color: white;
  text-align: left;
  font-size: 28px;
  text-shadow: 2px 2px 5px black;
}
ul {
  list-style-type: none;
}
<html>
  
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container-fluid">
    <div class="page-header">
      <h1 style="color: white;">Text Logo</h1>
    </div>
    <ul>
    <div class="center-block menu">
    <li> <p id="1">demo1</p></li>
     <li> <p id="2">demo2</p></li>
     <li> <p id="3">demo3</p></li>
     <li><p id="4">demo4</p><li>
    </div>
    </ul>
    
  </div>
<footer class="footer">
    <div class="container col-lg-6 col-md-6 col-sm-6 col-xs-6">
    <p class="text-muted">Company all rights reserved &copy; </p>
  </div>
</footer>
</body>
</html>

答案 1 :(得分:0)

具有类中心块的div不能垂直对齐,因为它不在具有更高高度的元素内。 &#34;容器流体&#34;没有高度,因此它将与其内部(中心块div)一样高。容器流体的父母(身体和html标签)也是如此。所以你需要将它包装在更高的容器中。我做了笔来说明。

http://codepen.io/ugreen/pen/GjBWWZ

神奇的部分是这个家伙:

.flex {
  display: flex;
  align-items: center;
  height: 100%;
  border: 1px solid red;
}