CSS3 给我们新增了选择器,可以更加便捷,更加自由的选择目标元素。
属性选择器、结构伪类选择器、伪元素选择器
一、属性选择器
属性选择器可以根据元素特定的属性来选择元素,这样就可以不用借助于类或者id选择器
选择符 | 简介 |
E[att] | 选择具有att属性的E元素 |
E[att=“val”] | 选择具有att属性且属性值等于val的E元素 |
E[att^=“val”] | 匹配具有att属性且值以val开头的E元素 |
E[att$=“val”] | 匹配具有att属性且值以val结尾的E元素 |
E[att*=“val”] | 匹配具有att属性且值中含有val的E元素 |
1. 利用属性选择器就可以不借助于类或者id选择器
<head>
<style>
input[value] {
color: pink;
}
</style>
</head>
<body>
<!-- 1.利用属性选择器可以不借助类或者id选择器 -->
<input type="text" value="请输入用户名">
<input type="text">
</body>
2. 属性选择器还可以选择 属性 = 值的某些元素
<head>
<style>
input[type=text] {
color: pink;
}
</style>
</head>
<body>
<!-- 2.属性选择器还可以选择 属性=值的某些元素 -->
<input type="text" name="" id="">
<input type="password" name="" id="">
</body>
3. 属性选择器可以选择属性值开头的某些元素
<head> /* 选择首先是div,然后具有class属性,并且是icon开头的值 */
<style>
div[class^=icon] {
color: pink;
}
</style>
</head>
<body>
<!-- 3.属性选择器可以选择属性值开头的某些元素 -->
<div class="icon1">小图标1</div>
<div class="icon2">小图标2</div>
<div class="icon3">小图标3</div>
<div class="icon4">小图标4</div>
</body>
4. 属性选择器可以选择属性值结尾的某些元素
<head>
<style>
section[class$=data] {
color: pink;
}
</style>
</head>
<body>
<!-- 4.属性选择器可以选择属性值结尾的某些元素 -->
<section class="icon1-data">1</section>
<section class="icon2-data">2</section>
<section class="icon3-data">3</section>
</body>
二、结构伪类选择器
结构伪类选择器主要根据文档结构来选择元素
常用于根据父级选择器选择里面的子元素
选择符 | 简介 |
E:first-child | 匹配父元素中的第一个子元素E |
E:last-child | 匹配父元素中最后一个E元素 |
E:nth-child(n) | 匹配父元素中的第n个子元素E |
E:first-of-type | 指定类型E的第一个 |
E:last-of-type | 指定类型E的最后一个 |
E:nth-of-type(n) | 指定类型E的第n个 |
①、E:first-child 和E:last-child
<head>
<style>
/* 1.选择ul里面的第一个孩子 小li */
ul li:first-child {
background-color: pink;
}
/* 2.选择ul里面的最后一个孩子 小li */
ul li:last-child {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
②、nth-child(n)
nth-child(n)选择某个父级元素的一个或多个特定的子元素(重点)
1. n可以是数字,关键字和公式
2. n如果是数字,就是选择第n个子元素,里面数字从1开始
3. n可以是关键字:even 偶数,odd奇数
4. n可以是公式:常见的公式如下(如果n是公式,则从0开始计算,但是第0个元素或者超出了元素的个数会被忽略)
公式 | 取值 |
2n | 偶数(等价于even) |
2n+1 | 奇数(等价于odd) |
5n | 奇数(等价于odd) |
n+5 | 从第5个开始(包含第五个)到最后 |
-n+5 | 前5个(包含第5个) |
<head>
<style>
/* 选择ul里面的第2个孩子 小li */
ul li:nth-child(2) {
background-color: pink;
}
/* 1.把所有的偶数 even的孩子选出来 */
ul li:nth-child(even) {
background-color: #ccc;
}
/* 2.把所有的奇数 odd的孩子选出来 */
ul li:nth-child(odd) {
background-color: gray;
}
/* 3.nth-child(n) 从0开始 每次加1 往后面计算 这里面必须是n 不能是其他的字母 选择了所有的孩子*/
/* ol li:nth-child(n) {
background-color: pink;
} */
/* 4.nth-child(2n)母选择了所有的偶数孩子 等价于 even*/
/* ol li:nth-child(2n) {
background-color: pink;
}
ol li:nth-child(2n+1) {
background-color: skyblue;
} */
/* ol li:nth-child(n+3) {
background-color: pink;
} */
ol li:nth-child(-n+3) {
background-color: pink;
}
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
</body>
③、E:first-of-type和E:last-of-type
E:first-of-type | 指定类型E的第一个 |
E:last-of-type | 指定类型E的最后一个 |
<head>
<style>
ul li:first-of-type {
background-color: pink;
}
ul li:last-of-type {
background-color: pink;
}
ul li:nth-last-child(2) {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
</body>
④、区别
nth-child(n)和nth-of-type(n)区别?
1. nth-child 对父元素里面所有孩子排序选择(序号是固定的),先找到第n个孩子,然后看看是否和E匹配
2. nth-of-type 对父元素里面指定子元素进行排序选择,先去匹配E,然后再根据E 找第n个孩子
⑤、小结
1. 结构伪类选择器一般用于选择父级里面的第几个孩子
2. nth-child 对父元素里面所有孩子排序选择(序号是固定的),先找到第n个孩子,然后看看是否和E匹配
3. nth-of-type 对父元素里面指定子元素进行排序选择,先去匹配E,再根据E找第n个孩子
4. 关于nth-child(n), 我们要知道n是从0开始计算的,要记住常用的公式
5. 如果是无序列表,我们肯定用 nth-child 更多
6. 类选择器,属性选择器,伪类选择器,权重为10
三、 伪元素选择器
伪元素选择器可以帮我们利用CSS创建新标签元素,而不需要HTML标签,从而简化HTML结构
::before:在元素内部的前面插入内容;::after:在元素内部的后面插入内容
注意:1. before 和 after 创建一个元素,但是是属于行内元素
before和after 都是一个盒子,都作为父元素的孩子
2. 新创建的这个元素在文档树中是找不到的,所以我们称为伪元素
语法:
element::before {
}
before是放在内容的前面,after是放在了内容的后面
<head>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
/* div::before 权重是2 */
div::before {
/* 这个content是必须要写的 */
content: '我';
}
div::after {
content: '暨南大学';
}
</style>
</head>
<div>
要
</div>
1. before 和 after 必须有 content 属性
2. before 在父元素内容的前面创建元素 ,after 在父元素内容的后面插入元素
3. 伪元素选择器 和 标签选择器 一样,权重为1