Flexbox div不在另一个div内

时间:2017-01-26 14:35:26

标签: css twitter-bootstrap css3 flexbox

我在另一个div.container内有一个div#zinc.container-fluid。我使用flexjustify-content属性的常用align-items框方法,但这似乎不起作用...我猜这是position: absolute的内容。

以下是我的所有代码:

body {
  font-family: Montserrat, sans-serif;
  font-weight: 200;
}

.navbar {
  border-style: none;
  border-radius: 0;
  background-color: transparent;
}
.navbar .navbar-header [type="button"].navbar-toggle {
  border-style: none;
  border-radius: 0;
}
.navbar .navbar-header .navbar-brand {
  color: #C70039;
}
.navbar .navbar-collapse {
  border-style: none;
  border-radius: 0;
}
.navbar .navbar-collapse ul {
  background-color: #FF5733;
}
.navbar .navbar-collapse ul a {
  color: #fff;
}

#zinc {
  background-color: blue;
  color: #fff;
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
#zinc #subtitle {
  font-size: 24px;
}

/*# sourceMappingURL=styles.css.map */
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Aravind Suresh</title>
    <link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <nav class="navbar navbar-default navbar-fixed-top">
      <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-navbar-collapse-" 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="#">Aravind Suresh</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-navbar-collapse-">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#zinc">Who Am I?</a></li>
            <li><a href="#">What do I do?</a></li>
            <li><a href="#">What are my likes?</a></li>
            <li><a href="#">Contact me</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    <div class="container-fluid" id="zinc">
      <div class="container">
        <h1>Aravind Suresh</h1>
        <span id="subtitle">I love code.</span>
      </div>
    </div>

    <script
			  src="http://code.jquery.com/jquery-3.1.1.js"
			  integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
			  crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </body>
</html>

如何将div.container放在div#zinc内?

P.S。请注意我在我的项目中使用Bootstrap 3。

2 个答案:

答案 0 :(得分:1)

通过Bootstrap重置width .container

#zinc > .container {
  width: auto;
}

答案 1 :(得分:0)

.container已经是水平和垂直居中,但它是一个块,所以孩子们正确地与块对齐。如果您希望孩子们也居中,考虑到他们都是文本,只需使用text-align: center

body {
  font-family: Montserrat, sans-serif;
  font-weight: 200;
}
.navbar {
  border-style: none;
  border-radius: 0;
  background-color: transparent;
}
.navbar .navbar-header [type="button"].navbar-toggle {
  border-style: none;
  border-radius: 0;
}
.navbar .navbar-header .navbar-brand {
  color: #C70039;
}
.navbar .navbar-collapse {
  border-style: none;
  border-radius: 0;
}
.navbar .navbar-collapse ul {
  background-color: #FF5733;
}
.navbar .navbar-collapse ul a {
  color: #fff;
}
#zinc {
  background-color: blue;
  color: #fff;
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
#zinc #subtitle {
  font-size: 24px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Aravind Suresh</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <nav class="navbar navbar-default navbar-fixed-top">
    <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-navbar-collapse-" 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="#">Aravind Suresh</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-navbar-collapse-">
        <ul class="nav navbar-nav navbar-right">
          <li><a href="#zinc">Who Am I?</a>
          </li>
          <li><a href="#">What do I do?</a>
          </li>
          <li><a href="#">What are my likes?</a>
          </li>
          <li><a href="#">Contact me</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>
  <div class="container-fluid" id="zinc">
    <div class="container">
      <h1>Aravind Suresh</h1>
      <span id="subtitle">I love code.</span>
    </div>
  </div>

  <script src="http://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>

</html>