为什么这个标题中心不显示?

时间:2018-07-07 15:29:14

标签: html css

我有一个带有标题的侧边栏,并且我想将标题居中放置在侧边栏的中央,以使标题中每个单词的第一个字母垂直对齐。

#sidebar {
  height: 100%;
  width: 250px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  overflow-x: hidden;
  padding-top: 20px;
  background: linear-gradient(-90deg, #353f4d, #3f4957);
}

#title-div {
  position: absolute;
  top: 0;
  width: 100%;
  background-color: #2980b9;
}

.title-text {
  display: inline-block;
  margin-left: auto;
  margin-right: auto;
  font-family: Raleway;
  color: #f2f1f1;
}
<body>
  <div id="sidebar">
    <div id="title-div">
      <h1 class="title-text">The<br>Title</h1>
    </div>
  </div>
  </body

CLICK HERE for the image

1 个答案:

答案 0 :(得分:0)

display: flex; justify-content: center for #title-div解决了该问题。

如果您不熟悉Flexbox,建议您使用this guide

摘要

#sidebar {
  height: 100%;
  width: 250px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  overflow-x: hidden;
  padding-top: 20px;
  background: linear-gradient(-90deg, #353f4d, #3f4957);
}

#title-div {
  display: flex;
  justify-content: center;
  position: absolute;
  top: 0;
  width: 100%;
  background-color: #2980b9;
}

.title-text {
  display: inline-block;
  margin-left: auto;
  margin-right: auto;
  font-family: Raleway;
  color: #f2f1f1;
}
<body>
  <div id="sidebar">
    <div id="title-div">
      <h1 class="title-text">The<br>Title</h1>
    </div>
  </div>
</body>