1、 简单选择器
| 序号 |
选择器 |
描述 |
举例 |
| 1 |
元素选择器 |
根据元素标签名称进行匹配 |
div {...} |
| 2 |
群组选择器 |
同时选择多个不同类型的元素 |
h1,h2,h3{...} |
| 3 |
通配选择器 |
选择全部元素,不区分类型 |
* {...} |
| 4 |
属性选择器 |
根据元素属性进行匹配 |
*[...] |
| 5 |
类选择器 |
根据元素 class 属性进行匹配 |
*.active {...} |
| 6 |
id 选择器 |
根据元素 id 属性进行匹配 |
*#top {...} |
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>简单选择器</title> <style> /* 类选择器 对应元素中的class;class选择器 */ .ul { background-color: lightgrey; font-size: 1.5rem; } .item { background-color: lightblue; } /* 元素选择器 */ body { background-color: lightgreen; } /* ID选择器 */ #five { background-color: lightpink; } /* 多个类选择器 */ .item.center { background-color: lightslategray; } /* 属性选择器 如果只有一个title属性,则可以不加后面的值="nine"*/ .item[title="nine"] { background-color: lightskyblue; } /* 选择多个设置背景颜色 */ #two, .item[title="three"], .item.four { background-color: mediumpurple; } </style> </head> <body> <ul class="ul"> <li class="item center">1</li> <li class="item" id="two">2</li> <li class="item" title="three">3</li> <li class="item four">4</li> <li class="item" id="five">5</li> <li class="item">6</li> <li class="item">7</li> <li class="item">8</li> <li class="item" title="nine">9</li> </ul> </body></html>
2、上下文选择器
- html 文档,看上去就像一颗倒置的”树”,所以是有层级结构的
- 每一个元素, 在文档中, 都有自己的位置,即上下文关系
- 所以, 完全可以根据元素的上下文关系,来获取到它们
2.1 一个元素的四种角色
| 序号 |
角色 |
描述 |
| 1 |
祖先元素 |
拥有子元素,孙元素等所有层级的后代元素 |
| 2 |
父级元素 |
仅拥有子元素层级的元素 |
| 3 |
后代元素 |
与其它层级元素一起拥有共同祖先元素 |
| 4 |
子元素 |
与其它同级元素一起拥有共同父级元素 |
2.2 四种上下文选择器
| 序号 |
选择器 |
操作符 |
描述 |
举例 |
| 1 |
后代选择器 |
空格 |
选择当前元素的所有后代元素 |
div p, body * |
| 2 |
父子选择器 |
> |
选择当前元素的所有子元素 |
div > h2 |
| 3 |
同级相邻选择器 |
+ |
选择拥有共同父级且相邻的元素 |
li.red + li |
| 4 |
同级所有选择器 |
~ |
选择拥有共同父级的后续所有元素 |
li.red ~ li |
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>上下文选择器</title> <style> body { background-color: mediumturquoise; } .uuul { background-color: lightgray; font-size: 1.5rem; } /* 后代选择器 */ .uuul li { background-color: lightgoldenrodyellow; } /* 同级相邻选择器 */ .item[title="five"] + .item { background-color: lightgreen; } /* 同级所有选择器 */ .item[title="five"] ~ .item { background-color: lightgreen; } /* 父子选择器 */ body > ul { border: 1px solid lightcoral; } </style> </head> <body> <ul class="uuul"> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> <li class="item" title="five">5</li> <li class="item">6</li> <li class="item">7</li> <li class="item">8</li> <li class="item">9</li> </ul> </body></html>
3、 伪类选择器
- 伪: 本意是假的,不存在的意思, 这里是特指, 不需要在元素上添加额外的属性来获取元素
- 类: 暗指伪类的级别, 仍然是属于”class”级别, 仍然属于属性选择器范畴,级别高于元素选择器
我们重点放在伪类最重要的应用场景:
| 场景 |
描述 |
| 结构伪类 |
根据子元素的位置特征进行选择 |
| 表单伪类 |
根据表单控件状态特征进行选择 |
3.1 结构伪类
3.1.1 不分组匹配
| 序号 |
选择器 |
描述 |
举例 |
| 1 |
:first-child |
匹配第一个子元素 |
div :first-child |
| 2 |
:last-child |
匹配最后一个子元素 |
div :last-child |
| 3 |
:only-child |
选择元素的唯一子元素 |
div :only-child |
| 4 |
:nth-child(n) |
匹配任意位置的子元素 |
div :nth-child(n) |
| 5 |
:nth-last-child(n) |
匹配倒数任意位置的子元素 |
div :nth-last-child(n) |
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>上下文选择器</title> <style> body { background-color: mediumturquoise; } .uuul { background-color: lightgray; font-size: 1.5rem; } /* 选择第一个 */ .uuul > :first-child { /* background-color: lightcyan; */ } /* 选择最后一个 */ .uuul > :last-child { /* background-color: lightgreen; */ } /* 选择任何位置(指定位置) 正序*/ .uuul > :nth-child(5) { /* background-color: lightpink; */ } /* 选择任何位置(指定位置)倒序 */ .uuul > :nth-last-child(2) { /* background-color: lightseagreen; */ } /* 选择奇数偶数【奇数:odd或2n-1;偶数:2n或even】 */ /* n是从0开始的,2n指2*0=0 2*1=2 2*2=4 2*3=6 ...... */ .uuul > :nth-child(2n) { /* background-color: lightskyblue; */ } /* 将不分组匹配看作是一个循环,在循环内判断传入的值(n)等于当前循环位置时,改变当前位置的背景颜色 */ /* 案例:选择前三 */ /* 解析: */ /* -0 + 3 = 3 开始循环*/ /* -1+3=2 */ /* -2+3=1 */ /* -3+3=0 临界点结束循环*/ .uuul > :nth-child(-n + 3) { background-color: lime; } /* 案例:选择从5开始后面的所有item */ /* 解析 */ /* 0+5=5 开始循环 */ /* 1+5=6 */ /* 2+5=7 */ /* 3+5=8 */ /* 4+5=9 item只有9个;最大值,临界点结束*/ .uuul .item:nth-child(n + 5) { background-color: magenta; } </style> </head> <body> <ul class="uuul"> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> <li class="item" title="five">5</li> <li class="item">6</li> <li class="item">7</li> <li class="item">8</li> <li class="item">9</li> <li class="item222">10</li> </ul> </body></html>
3.1.2 分组匹配
| 序号 |
选择器 |
描述 |
举例 |
| 1 |
:first-of-type |
匹配按类型分组后的第一个子元素 |
div :first-of-type |
| 2 |
:last-of-type |
匹配按类型分组后的最后一个子元素 |
div :last-of-type |
| 3 |
:only-of-type |
匹配按类型分组后的唯一子元素 |
div :only-of-type |
| 4 |
:nth-of-type() |
匹配按类型分组后的任意位置的子元素 |
div :nth-of-type(n) |
| 5 |
:nth-last-of-type() |
匹配按类型分组后倒数任意位置的子元素 |
div :nth-last-of-type(n) |
- 允许使用表达式来匹配一组元素,表达式中的”n”是从”0”开始计数,且必须写到前面
- “-n”表示获取前面一组元素,正数表示从指定位置获取余下元素
- 学习实例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>上下文选择器</title> <style> body { background-color: mediumturquoise; } .uuul { background-color: lightgray; font-size: 1.5rem; } /* 选择第一个 */ .uuul li:first-of-type { background-color: maroon; } /* 选择最后一个 */ .uuul li:last-of-type { background-color: mediumaquamarine; } /* 选择指定位置 */ .uuul li:nth-of-type(2) { background-color: mediumorchid; } </style> </head> <body> <ul class="uuul"> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> </ul> </body></html>
3.3 其它伪类
| 序号 |
选择器 |
描述 |
| 1 |
:active |
向被激活的元素添加样式 |
| 2 |
:focus |
向拥有键盘输入焦点的元素添加样式 |
| 3 |
:hover |
当鼠标悬浮在元素上方时,向元素添加样式 |
| 4 |
:link |
向未被访问的链接添加样式 |
| 5 |
:visited |
向已被访问的链接添加样式 |
| 5 |
:root |
根元素,通常是html |
| 5 |
:empty |
选择没有任何子元素的元素(含文本节点) |
| 5 |
:not() |
排除与选择器参数匹配的元素 |
总结
- 伪类选择器是最重要的部分,我把他当做一个循环,在循环内判断传入的值(n)等于当前循环位置时,可以改变当前位置的背景颜色或者处理其他
- 简单选择器中类选择器 class 和 id 选择器是最常用的
批改老师:
天蓬老师
批改状态:合格
老师批语:选择器是css的核心基础知识