gpt4 book ai didi

html - 将鼠标悬停在第一个 child 无法正常工作的链接上

转载 作者:行者123 更新时间:2023-12-04 14:05:36 32 4
gpt4 key购买 nike

所以,我只想让导航栏的第一个元素在我将鼠标悬停在它上面时更改它的背景颜色,但是“主页”和“新闻”按钮都会改变颜色,即使我写了 a :first-child:hover.请帮忙。

<!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>

最佳答案

:first-child 应该给li,而不是a

因为在每个li中只有一个a的 child ,但是在ul中有多个li 。所以 :first-child 应该给 li,然后悬停到 a

  #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:first-child a:hover{
background-color: red;
}
<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>

关于html - 将鼠标悬停在第一个 child 无法正常工作的链接上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68634512/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com