复合选择器
一、后代选择器
后代选择器又称为包含选择器。用来选择元素和元素组的子孙后代。
语法:
父级 子级{属性1:属性值1;属性2:属性值2}
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.nav a{
color: aquamarine;
text-decoration: none;
}
</style>
</head>
<body>
<div class="nav">
<a href="#">内部链接</a>
<a href="#">内部链接</a>
<a href="#">内部链接</a>
</div>
<a href="#">外部链接</a>
<a href="#">外部链接</a>
<a href="#">外部链接</a>
</body>
</html>
二、子元素选择器
子元素选择器只能选择作为某元素亲儿子的元素。
语法:
父级>子级{属性1:属性值1;属性2:属性值2;}
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div>strong{
color: blue;
}
</style>
</head>
<body>
<div>
<strong>儿子</strong>
<strong>儿子</strong>
<strong>儿子</strong>
</div>
<div>
<p>
<strong>孙子</strong>
<strong>孙子</strong>
<strong>孙子</strong>
</p>
</div>
</body>
</html>
三、交集选择器
交集选择器由两个选择器构成,找到的标签必须同时具备两个标签的特点。
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p.red{
color: crimson;
}
</style>
</head>
<body>
<p class="red">红色</p>
<p class="red">红色</p>
<p class="red">红色</p>
<div class="red">红色</div>
<div class="red">红色</div>
<div class="red">红色</div>
<p>蓝色</p>
<p>蓝色</p>
<p>蓝色</p>
</body>
</html>
四、并集选择器
如果某些选择器定义的样式完全相同,或者部分相同,就可以利用并集选择器为他们定义相同的CSS样式。
并集选择器是各个选择器通过“ , ”连接而成。
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p,div{
color: cyan;
}
</style>
</head>
<body>
<p>红色</p>
<p>红色</p>
<p>红色</p>
<div>紫色</div>
<div>紫色</div>
<div>紫色</div>
<span>蓝色</span>
<span>蓝色</span>
<span>蓝色</span>
</body>
</html>