我想制作一个网站。我决定让导航栏有一个固定的位置,所以当我向下滚动时,我总是可以看到它,但是当我将nav属性添加到属性位置时:固定它就会消失。不能'弄清楚发生了什么。有人可以解释,我做错了什么?非常感谢你! P.S:我是新来的编码。css导航栏位置固定不起作用
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>
谢谢大家对你的答案和你的时间。在我将top,left和right属性设置为0之后,一切正常。看起来我有另一个指定了z-index的元素,它搞乱了一切。我改变了nav元素的z-index值,现在一切正常。 – Andrei
将'left'和'right'设置为0是'width:100%'的另一种选择(正如我在inmy answer中所写的),但这两种方法都会为固定元素定义宽度。 'left:0'''不会起作用,顺便说一句... – Johannes