7、结构伪类选择器
2.3、结构伪类选择器
代码show
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 避免使用,class,id选择器-->
<style>
/*ul的第一个子元素*/
ul li:first-child {
background: #80342a;
}
/*ul的最后一个子元素*/
ul li:last-child {
background: red;
}
/*选中p1:定位到父级元素,选中当前的第一个元素
选中当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效! 顺序*/
p:nth-child(2) {
background: orange;
}
/*选中父元素,下的p元素的第二个,类型*/
p:nth-of-type(2) {
background: pink;
}
a:hover {
background: #11800e;
}
</style>
</head>
<body>
<a href="www.baidu.com">百度一下</a>
<h1>h1</h1>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
效果图:
彩蛋
1.看淘宝超链接放上去的颜色
修改为黄色: