前言
前面我有分享到学习到了3个选择器,这里给大家分享一下其他的选择器。
相邻选择器
相邻选择器是指
选择某个元素后面相邻的元素
,运用到的符号是
+
。下面通过实例来演示一下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
border: 1px solid red;
}
.box+p{
color: red;
}
</style>
</head>
<body>
<div class="box">box1</div>
<p>p1</p>
<p>p2</p>
</body>
</html>
上面的代码,需求是我们只需要改变
p1
的颜色,如果用
标签选择器
选择的范围就会很大也符合我们的需求,所以我们用相邻选择器就写出我们需要的效果。
需要的注意的是:相邻选择器只对两个相邻的元素有效。