将元素垂直对中在固定高度的容器中

时间:2017-05-14 01:13:27

标签: html css css3 flexbox css-position

我的网站需要基于像素的导航栏 - 在这种情况下为height: 80px

问题是,我无法将ulli元素垂直居中。

我尝试过使用:top:50%; transform: translate(0,-50%),如我的jsfiddle所示,还有flex定位,但没有一个有效。

Jsfiddle:https://jsfiddle.net/agreyfield91/w3s8cj92/

header {
  height: 80px;
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
  width: 100%;
}

footer {
  background: #ddd;
}

* {
  color: transparent
}

nav {
  height: 100%;
  width: 100%;
  background-color: bisque;
}

nav ul {
  list-style: none;
  text-align: center;
  margin: 0;
  padding: 0;
  top: 50%;
  transform: translate(0, -50%);
}

nav ul li {
  display: inline-block;
  float: left;
}

nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  margin: 0 10px;
}
<header class="nav-down">
  <nav class="headernav">
    <ul>
      <li><a href="#">Gig</a></li>
      <li><a href="#">ity</a></li>
      <li><a href="#">element</a></li>
    </ul>
  </nav>
</header>

5 个答案:

答案 0 :(得分:2)

添加display flex和align-self center:https://jsfiddle.net/w3s8cj92/1/

header {
  height: 80px;
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
  width: 100%;
}

footer {
  background: #ddd;
}

* {
  color: transparent
}

nav {
  height: 100%;
  width: 100%;
  background-color: bisque;
  display: flex;
}

nav ul {
  list-style: none;
  text-align: center;
  margin: 0;
  padding: 0;
  top: 50%;
  align-self: center;
}

nav ul li {
  display: inline-block;
  float: left;
}

nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  margin: 0 10px;
}
<header class="nav-down">
  <nav class="headernav">
    <ul>
      <li><a href="#">Gig</a></li>
      <li><a href="#">ity</a></li>
      <li><a href="#">element</a></li>
    </ul>
  </nav>
</header>

答案 1 :(得分:1)

您需要将position: relative添加到nav,将position: absolute添加到nav ul(以使top: 50%; transform: translate(0,-50%);正常工作),或者您可以使用{{1在display: flex; align-items: center上。我只想使用flexbox。

&#13;
&#13;
nav
&#13;
header {
  height: 80px;
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
  width: 100%;
}

footer {
  background: #ddd;
}

* {
  color: transparent
}

nav {
  height: 100%;
  width: 100%;
  background-color: bisque;
  display: flex;
  align-items: center;
}

nav ul {
  list-style: none;
  text-align: center;
  margin: 0;
  padding: 0;
}

nav ul li {
  display: inline-block;
  float: left;
}

nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  margin: 0 10px;
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

看起来您的代码可能更简单:

&#13;
&#13;
nav {
  height: 80px;
  position: fixed;
  top: 0;
  width: 100%;
  display: flex;
  justify-content: center;   /* horizontal alignment of child elements (optional) */
  background-color: bisque;
}

nav a {
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  display: flex;
  align-items: center;      /* vertical alignment of text */
  border: 1px dashed red;
}

a + a {
  margin-left: 20px;
}
&#13;
<nav>
  <a href="#">Gig</a>
  <a href="#">ity</a>
  <a href="#">element</a>
</nav>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果你有块的静态高度,可以将带有无包装容器的元素居中,垂直放置在中间对齐的内联[-block]元素中。

.vertical-container {
  height: 80px;
  border: 1px solid black;
  white-space: nowrap;
  /* this prevents vertical block to "fall out" of container */
}

.vertical-aligner {
  display: inline-block;
  vertical-align: middle;
  height: 100%;
  width: 0;
}

.vertical-content {
  display: inline-block;
  vertical-align: middle;
  border: 1px solid red;
  white-space: normal;
  /* reset white-space for content */
}
<div class="vertical-container"><span class="vertical-aligner"></span><span class="vertical-content">Text that should be vertically middle of container</span></div>

我已相应地更新了你的小提琴:https://jsfiddle.net/w3s8cj92/2/

P.S。我想提供更多信息,但稍后

答案 4 :(得分:0)

CSS替换

header {
    height: 80px;
    position: fixed;
    top: 0;
    transition: top 0.2s ease-in-out;
    width: 100%;
    display: table;
}

footer { background: #ddd;}
* { color: transparent}
nav {
    height: 100%;
  width: 100%;
  background-color: bisque;
}
nav ul {
    margin: 0;
    padding: 0;
    display: table-cell;
    vertical-align: middle;
    height: 80px;
}
nav ul li {
  display: inline-block;
    float: left;
}
nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  margin: 0 10px;
}