如何在div中居h1?

时间:2017-08-29 16:01:57

标签: html css css3 css-transforms centering

我设置了" 顶部"和" "到" 50%"和" 职位"到" absolute" - 当窗口最小化时,.supporting h1仅以div为中心。我认为将位置设置为绝对不会那样做。当窗口放大时,.supporting h1移动到div的底部。

我该如何解决这个问题?对我来说还是html / css和学习的新手!

not centered



html,
body {
  font-family: ;
  margin: 0 auto;
  text-transform: lowercase;
}

.container {
  margin: 0 auto;
  padding: 0 !important;
}

.nav {
  float: right;
  display: inline;
  margin: 0 auto;
  list-style: none;
}

.nav li {
  list-style: none;
  display: inline;
  padding-top: 0;
  padding-left: 10px;
}

.nav>li>a {
  padding: 0px !important;
  color: black;
}

a:hover {
  background-color: yellow;
}

.header {
  width: 100%;
  display: inline-block;
  padding: 10px 0 0 0;
  margin: 0 auto;
}

.header h1 {
  font-size: 20px;
  margin: 0 auto;
  display: inline-block;
}

.header .nav {
  padding-left: 10px;
}

.supporting h1 {
  border-left: 0px solid black;
  padding: 10px;
  margin: 0 auto;
  display: inline-block;
  top: 50%;
  left: 50%;
  position: absolute;
}

.supporting .container-fluid {
  background: rgba(225, 225, 225, .8);
  height: 500px;
}

.work .container-fluid {
  height: 500px;
  margin: 0 auto;
  background: white);
  width: 100%;
  padding: 0;
}

div.container-fluid {
  padding: 0;
  width: 100%;
  height: 100%;
}

<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/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="container">
    <div class="header">
      <h1 class="logo">header</h1>
      <ul class="nav nav-pills">
        <li><a href="#">Projects</a></li>
        <li><a href="#">Photography</a></li>
        <li><a href="#">blog</a></li>
      </ul>
    </div>
  </div>
  <div class="supporting">
    <div class="container-fluid">
      <div class="container">
        <h1>hi.</h1>
      </div>
    </div>
  </div>
  <div class="work">
    <div class="container-fluid">
      <div class="container">
        <h1>work</h1>
      </div>
    </div>
  </div>
</div> //added this div
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

如果您想使用绝对定位position: relative添加到supporting div,并将transform: translate(-50%, -50%)添加到h1标记。

见下面的演示:

html,
body {
  font-family: ;
  margin: 0 auto;
  text-transform: lowercase;
}

.container {
  margin: 0 auto;
  padding: 0 !important;
}

.nav {
  float: right;
  display: inline;
  margin: 0 auto;
  list-style: none;
}

.nav li {
  list-style: none;
  display: inline;
  padding-top: 0;
  padding-left: 10px;
}

.nav>li>a {
  padding: 0px !important;
  color: black;
}

a:hover {
  background-color: yellow;
}

.header {
  width: 100%;
  display: inline-block;
  padding: 10px 0 0 0;
  margin: 0 auto;
}

.header h1 {
  font-size: 20px;
  margin: 0 auto;
  display: inline-block;
}

.header .nav {
  padding-left: 10px;
}
.supporting {
  position: relative;
}
.supporting h1 {
  border-left: 0px solid black;
  padding: 10px;
  margin: 0 auto;
  display: inline-block;
  top: 50%;
  left: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
}

.supporting .container-fluid {
  background: rgba(225, 225, 225, .8);
  height: 500px;
}

.work .container-fluid {
  height: 500px;
  margin: 0 auto;
  background: white);
  width: 100%;
  padding: 0;
}

div.container-fluid {
  padding: 0;
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container-fluid">
  <div class="container">
    <div class="header">
      <h1 class="logo">header</h1>
      <ul class="nav nav-pills">
        <li>
          <a href="#">Projects</a>
        </li>
        <li>
          <a href="#">Photography</a>
        </li>
        <li>
          <a href="#">blog</a>
        </li>
      </ul>
    </div>
  </div>

  <div class="supporting">
    <div class="container-fluid">
      <div class="container">
        <h1>hi.</h1>
      </div>
    </div>
  </div>

  <div class="work">
    <div class="container-fluid">
      <div class="container">
        <h1>work</h1>
      </div>
    </div>
  </div>

答案 1 :(得分:1)

你不能在容器内使用容器..上面你使用了容器 - 流体,在你使用容器的div里面。这是错的。看看文档。你不能在容器内使用容器