我想让导航栏的第一个元素在鼠标悬停时改变其背景颜色,但是即使我写了a:first-child:hover
,"Home"和"News"按钮都会改变颜色。请帮忙。
<!DOCTYPE html>
<html>
<head>
<style>
html, nav, ul, li, a, span{
font-family: regular;
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.nav, header{
display: block;
}
ol, ul{
list-style: none;
}
a{
text-decoration: none;
}
#header h1 {
left: 1.25em;
margin: 0;
position: absolute;
}
#header {
background: rgba(39, 40, 51, 0.965);
box-shadow: 0 0 0.25em 0 rgba(0, 0, 0, 0.25);
width: 100%;
height: 3.5em;
left: 0;
top: 0;
line-height: 3.5em;
position: fixed;
z-index: 100;
}
#header a, #header a:visited{
color: rgba(224, 224, 224, 0.986);
-o-transition: 0.5s;
-ms-transition: 0.5s;
-moz-transition: 0.5s;
-webkit-transition: 0.5s;
transition: 0.5s;
font-size: 125%;
}
#header nav{
position: absolute;
right: 1em;
top: 0;
}
#header nav ul li{
display: inline-block;
margin-left: 1em;
}
#header nav ul li a:first-child:hover{
background-color: red;
}
</style>
</head>
<body>
<header id="header">
<h1 id="logo"><a href="#">Random</a></h1>
<nav id="nav">
<ul>
<li><a href="">Home</a></li>
<li><a href="#2">News</a></li>
</ul>
</nav>
</header>
</body>
</html>
li
上,选择父级ul
中的第一个li
。 - CBroe