nth-child 无法在 ul 中工作
nth-child not working in ul
我有一个简单的 <nav>
,里面有一个无序列表:
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li a:nth-child(1):hover {
background: red;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>
我试图在悬停时为每个 child <a>
制作不同的颜色
但它选择了所有这些(将它们突出显示为红色)
nav li a:nth-child(1):hover {
background: red;
}
我做错了什么?
你所有的A
都是他们父元素的第一个元素。您必须在 LI
元素上应用 nth-child
,而不是 A
:
nav li:nth-child(1) a:hover {
background: red;
}
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li:nth-child(1) a:hover {
background: red;
}
nav li:nth-child(2) a:hover {
background: #555;
}
nav li:nth-child(3) a:hover {
background: green;
}
nav li:nth-child(4) a:hover {
background: blue;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li:nth-child(1) a:hover {
background: green;
}
nav li:nth-child(2) a:hover {
background: blue;
}
nav li:nth-child(3) a:hover {
background: pink;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>
我有一个简单的 <nav>
,里面有一个无序列表:
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li a:nth-child(1):hover {
background: red;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>
我试图在悬停时为每个 child <a>
制作不同的颜色
但它选择了所有这些(将它们突出显示为红色)
nav li a:nth-child(1):hover {
background: red;
}
我做错了什么?
你所有的A
都是他们父元素的第一个元素。您必须在 LI
元素上应用 nth-child
,而不是 A
:
nav li:nth-child(1) a:hover {
background: red;
}
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li:nth-child(1) a:hover {
background: red;
}
nav li:nth-child(2) a:hover {
background: #555;
}
nav li:nth-child(3) a:hover {
background: green;
}
nav li:nth-child(4) a:hover {
background: blue;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>
nav {
position: absolute;
right: 0px;
white-space: nowrap;
}
nav ul {
padding: 0;
margin: 0;
}
nav ul li {
clear: both;
white-space: nowrap;
color: white;
display: inline-block;
}
nav ul li a {
color: white;
background: black;
text-decoration: none;
text-align: center;
font-family: statellite;
padding-left: 25px;
padding-right: 25px;
height: 37px;
margin-left: -4px;
padding-top: 18px;
display: inline-block;
}
nav ul li a:hover {
background: #000;
}
nav li:nth-child(1) a:hover {
background: green;
}
nav li:nth-child(2) a:hover {
background: blue;
}
nav li:nth-child(3) a:hover {
background: pink;
}
<nav>
<ul>
<li><a href="#">HOME</a>
</li>
<li><a href="#music">MUSIC</a>
</li>
<li><a href="#livestream">LIVESTREAM</a>
</li>
<li><a href="#links">LINKS</a>
</li>
<li><a href="#about">ABOUT</a>
</li>
</ul>
</nav>