我有一個需要基於像素的導航欄的網站 - 在這種情況下爲height: 80px
。在固定高度的容器中垂直居中元素
問題是,我不能將ul
和li
元素垂直居中。
我試過使用: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>
所有你需要的是增加'的位置是:相對的; overflow:hidden;'到'nav ul'規則。 「位置:相對;」使變換工作和「溢出:隱藏;」使其從子項(浮動並需要清除)中獲得高度。 [** Fiddle demo **](https://jsfiddle.net/LGSon/w3s8cj92/3/) – LGSon