滚动时如何让导航栏固定在屏幕顶部?
How do I get my nav bar to stick to the top of the screen when you scroll?
我正在尝试使用 overflow: sticky;为了让我的导航栏在滚动时停在屏幕顶部但它没有任何改变,我看到有人说将 ul 上的边距和填充设置为 0 我已经完成但没有改变,我也试过摆脱溢出:隐藏但是摆脱了酒吧的背景颜色。 HTML 和 CSS 下面。
HTML
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="Assets/css/site.css">
</head>
<body>
<header>
<img src = "Assets/images/Header1.jpg" id = "headImage">
<nav id = "navBar">
<ul>
<li><a href = "index.html" class = "active">Home</a></li>
<li><a href = "weapons.html">Weapons</a></li>
<li><a hred = "maps.html">Maps</a></li>
<li><a href = "modes.html">Modes</a></li>
<li><a href = "contact.html">Contact</a></li>
</ul>
</nav>
</header>
<footer>
</footer>
</body>
CSS
body{
background-color: rgb(43, 23, 23);
padding: 0;
margin: 0;
height: 1000px;
}
.active{
background-color: rgb(31, 31, 31);
}
#headImage{
width: 100%;
height: 150px;
object-fit: cover;
object-position: 50% 50%;
}
#navBar ul{
list-style-type: none;
overflow: hidden;
background-color: rgb(20, 20, 20);
padding: 0;
margin: 0;
position: sticky;
position: -webkit-sticky;
top: 0;
}
#navBar li{
float: left;
}
#navBar a{
display:block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navBar a:hover{
background-color: rgb(31, 31, 31);
}
只需从 header 中取出图像和 ul 就可以了。
很多可以修复您的代码的东西不一定与您的代码有关。
必填:
- 删除
-webkit-sticky;
因为它没用。 Firefox 支持 sticky
作为所有其他浏览器的默认设置,IE 除外。使用 -webkit- 已经过时很多年了。
- 它们
sticky
应该应用于 navbar
本身,而不仅仅是列表。
- 导航栏需要从 header 中排除。如果它是
header
的child,它会被强制留在child内,从而被拉出屏幕。
可选:
将 float
用于样式目的不仅过时而且从来都不是一回事。许多人出于其实际目的mis-used。它应该只用于段落中的浮动图像。对于该用途,只需将列表删除为 inline-block
。然后就可以和text-align
.
对齐了
body {
background-color: rgb(43, 23, 23);
padding: 0;
margin: 0;
height: 1000px;
}
.active {
background-color: rgb(31, 31, 31);
}
#headImage {
width: 100%;
height: 150px;
object-fit: cover;
object-position: 50% 50%;
}
#navBar {
position: sticky;
top: 0;
}
#navBar ul {
list-style-type: none;
overflow: hidden;
background-color: rgb(20, 20, 20);
padding: 0;
margin: 0;
}
#navBar li {
display: inline-block;
}
#navBar a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navBar a:hover {
background-color: rgb(31, 31, 31);
}
<header>
<img src="Assets/images/Header1.jpg" id="headImage">
</header>
<nav id="navBar">
<ul>
<li><a href="index.html" class="active">Home</a></li>
<li><a href="weapons.html">Weapons</a></li>
<li><a hred="maps.html">Maps</a></li>
<li><a href="modes.html">Modes</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
<footer>
</footer>
我正在尝试使用 overflow: sticky;为了让我的导航栏在滚动时停在屏幕顶部但它没有任何改变,我看到有人说将 ul 上的边距和填充设置为 0 我已经完成但没有改变,我也试过摆脱溢出:隐藏但是摆脱了酒吧的背景颜色。 HTML 和 CSS 下面。
HTML
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="Assets/css/site.css">
</head>
<body>
<header>
<img src = "Assets/images/Header1.jpg" id = "headImage">
<nav id = "navBar">
<ul>
<li><a href = "index.html" class = "active">Home</a></li>
<li><a href = "weapons.html">Weapons</a></li>
<li><a hred = "maps.html">Maps</a></li>
<li><a href = "modes.html">Modes</a></li>
<li><a href = "contact.html">Contact</a></li>
</ul>
</nav>
</header>
<footer>
</footer>
</body>
CSS
body{
background-color: rgb(43, 23, 23);
padding: 0;
margin: 0;
height: 1000px;
}
.active{
background-color: rgb(31, 31, 31);
}
#headImage{
width: 100%;
height: 150px;
object-fit: cover;
object-position: 50% 50%;
}
#navBar ul{
list-style-type: none;
overflow: hidden;
background-color: rgb(20, 20, 20);
padding: 0;
margin: 0;
position: sticky;
position: -webkit-sticky;
top: 0;
}
#navBar li{
float: left;
}
#navBar a{
display:block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navBar a:hover{
background-color: rgb(31, 31, 31);
}
只需从 header 中取出图像和 ul 就可以了。
很多可以修复您的代码的东西不一定与您的代码有关。
必填:
- 删除
-webkit-sticky;
因为它没用。 Firefox 支持sticky
作为所有其他浏览器的默认设置,IE 除外。使用 -webkit- 已经过时很多年了。 - 它们
sticky
应该应用于navbar
本身,而不仅仅是列表。 - 导航栏需要从 header 中排除。如果它是
header
的child,它会被强制留在child内,从而被拉出屏幕。
可选:
将 float
用于样式目的不仅过时而且从来都不是一回事。许多人出于其实际目的mis-used。它应该只用于段落中的浮动图像。对于该用途,只需将列表删除为 inline-block
。然后就可以和text-align
.
body {
background-color: rgb(43, 23, 23);
padding: 0;
margin: 0;
height: 1000px;
}
.active {
background-color: rgb(31, 31, 31);
}
#headImage {
width: 100%;
height: 150px;
object-fit: cover;
object-position: 50% 50%;
}
#navBar {
position: sticky;
top: 0;
}
#navBar ul {
list-style-type: none;
overflow: hidden;
background-color: rgb(20, 20, 20);
padding: 0;
margin: 0;
}
#navBar li {
display: inline-block;
}
#navBar a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navBar a:hover {
background-color: rgb(31, 31, 31);
}
<header>
<img src="Assets/images/Header1.jpg" id="headImage">
</header>
<nav id="navBar">
<ul>
<li><a href="index.html" class="active">Home</a></li>
<li><a href="weapons.html">Weapons</a></li>
<li><a hred="maps.html">Maps</a></li>
<li><a href="modes.html">Modes</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
<footer>
</footer>