【发布时间】:2020-12-04 17:08:03
【问题描述】:
我正在尝试使用溢出:粘性;为了让我的导航栏在滚动时停止在屏幕顶部但它没有任何改变,我看到人们说将 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);
}
【问题讨论】:
-
你不需要使用
webkitforsticky.Sticky是默认所有浏览器都支持的,除了 IE。不要将float用于样式目的。它只能用于段落中的浮动图像。您可以使用flexboxes或简单地将列表元素声明为inline并将其与text-align对齐。在 nabar itel 上使用position: sticky;而不是在ul上。
标签: html css position nav sticky