子元素选择器

来源:互联网 发布:1api域名注册 编辑:程序博客网 时间:2024/06/03 18:04
<!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>
原创粉丝点击