<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
/*
* 为第一个标签设置一个背景颜色为黄色
* :first-child 可以选中第一个子元素
* :last-child 可以选中最后一个子元素
*
* p:last-child{
* background-color: yellow;
* }
*
* :nth-child 可以选中任意位置的子元素
* 该选择器后面可以指定一个参数,指定要选则的第几个元素
* even 表示偶数位置的子元素
* odd 表示奇数位置的子元素
*/
p:nth-child(2){
background-color: pink;
}
/*
* :first-of-type
* :last-of-type
* :nth-of-type
* 和:first-child这些非常类似
* 只不过child是在所有子元素中排列
* 而type是在当前类型的子元素中排序
*/
p:nth-of-type(4){
background-color: skyblue;
}
/*
* 否定伪类:
* 作用:可以从已选的元素中剔除某些元素
* 语法:
* :not(选择器)
*/
p:not(.a){
background-color:yellow;
}
</style>
</head>
<body>
<span>qwqwwqw</span>
<p>我是一个段落</p>
<p>我是一个段落</p>
<p class="a">我是一个段落</p>
<p class="a">我是一个段落</p>
<p>我是一个段落</p>
<p>我是一个段落</p>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
/*
* 为第一个标签设置一个背景颜色为黄色
* :first-child 可以选中第一个子元素
* :last-child 可以选中最后一个子元素
*
* p:last-child{
* background-color: yellow;
* }
*
* :nth-child 可以选中任意位置的子元素
* 该选择器后面可以指定一个参数,指定要选则的第几个元素
* even 表示偶数位置的子元素
* odd 表示奇数位置的子元素
*/
p:nth-child(2){
background-color: pink;
}
/*
* :first-of-type
* :last-of-type
* :nth-of-type
* 和:first-child这些非常类似
* 只不过child是在所有子元素中排列
* 而type是在当前类型的子元素中排序
*/
p:nth-of-type(4){
background-color: skyblue;
}
/*
* 否定伪类:
* 作用:可以从已选的元素中剔除某些元素
* 语法:
* :not(选择器)
*/
p:not(.a){
background-color:yellow;
}
</style>
</head>
<body>
<span>qwqwwqw</span>
<p>我是一个段落</p>
<p>我是一个段落</p>
<p class="a">我是一个段落</p>
<p class="a">我是一个段落</p>
<p>我是一个段落</p>
<p>我是一个段落</p>
</body>
</html>