【发布时间】:2016-11-20 21:21:06
【问题描述】:
如果我将position: fixed 添加到nav 的CSS 代码中,导航栏的宽度与容器的宽度不同,有人可以帮我吗?
我的 HTML:
<body>
<div id="container">
<nav>
<ul>
<li><a href="" class="active">Home</a></li>
<li><a href="guestbook.html">Guestbook</a></li>
<li><a href="contact.html">Contact</a></li>
<li id="login" ><a href="logn.html">Login</a></li>
</ul>
</nav>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
<p>Example text</p>
</div>
</body>
我的 CSS:
body {
background-image: url('img/background.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
margin-top: 0px;
font-family: Arial;
}
#container {
background-color:#D3D3D3;
width: 70%;
margin: auto;
overflow:hidden;
}
nav ul {
list-style-type: none;
margin: 0;
overflow: hidden;
background-color: grey;
}
li {
float:left;
}
#login {
float: right;
padding-right: 40px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #A1D490;
}
.active {
background-color: #D3D3D3;
color: grey;
}
如果我将它添加到 CSS 中,导航栏的宽度就是其内容的宽度:
nav {
position: fixed;
}
有人可以帮帮我吗? 我在不同的网站上搜索过,似乎没有什么对我有用。
【问题讨论】:
-
nav { position: fixed; left: 0; right: 0; }
标签: html css css-position nav