【发布时间】:2017-03-08 19:16:04
【问题描述】:
我正在尝试制作一个网站。我决定让导航栏有一个固定的位置,所以当我向下滚动时,我总是可以看到它,但是当我添加到导航元素时,属性 position:fixed 它就消失了。无法弄清楚发生了什么。有人可以解释一下,我做错了什么吗?非常感谢! P.S:我是编码新手。
HTML 和 CSS
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
}
header nav #logo {
width: 140px;
position: absolute;
top: 15px;
}
nav {
position: relative;
background-color: #242628;
height: 70px;
padding-left: 20px;
}
nav ul {
position: absolute;
height: 100%;
margin: auto;
top: 0;
bottom: 0;
width: 600px;
padding-left: 200px;
}
nav ul li {
-moz-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
-o-transition: all 0.2s linear;
transition: all 0.2s linear;
display: inline;
float: left;
height: inherit;
width: 100px;
border-right: 1px solid gray;
}
nav ul li:hover {
background-color: rgba(12, 240, 255, 0.3);
}
nav ul li a {
color: white;
text-decoration: none;
position: absolute;
height: inherit;
width: inherit;
text-align: center;
padding-top: 25px;
}
<header>
<nav>
<img id="logo" src="images/logo.png" alt="logo" />
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Rate it!</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">Videos</a></li>
</ul>
</nav>
</header>
【问题讨论】:
-
感谢大家的回答和时间。在我将顶部、左侧和右侧属性设置为 0 后,一切正常。似乎我有另一个指定了 z-index 的元素搞砸了一切。我更改了导航元素的 z-index 值,现在一切正常。
-
将
left和right设置为 0 可以替代width: 100%(正如我在我的回答中所写的那样),但这两种方法都具有为固定元素定义宽度的效果。left: 0单独是行不通的,顺便说一句...