批改状态:合格
老师批语:选择器, 博大精深, 还好, 常用的并不多
元素选择器,根据元素标签名称进行匹配
语法举例:h1 {background-color: lightskyblue;}
群组选择器,同时选择多个不同类型的元素
语法举例:h1,h2,h3 {background-color: lightskyblue;}
通配选择器,选择全部元素,不区分类型
语法举例:* {color: white;}
类选择器,根据元素 class 属性进行匹配
语法举例:*.active {background-color: lightcoral;}
id 选择器,根据元素 id 属性进行匹配
语法举例:*#top {color: white;}
*“可以省略| 序号 | 角色 | 描述 |
|---|---|---|
| 1 | 祖先元素 | 拥有子元素,孙元素等所有层级的后代元素 |
| 2 | 父级元素 | 仅拥有子元素层级的元素 |
| 3 | 后代元素 | 与其它层级元素一起拥有共同祖先元素 |
| 4 | 子元素 | 与其它同级元素一起拥有共同父级元素 |
后代选择器,选择当前元素的所有后代元素
语法举例:body * {color: lightcoral;}
父子选择器,选择当前元素的所有子元素
语法举例:div > h2 {color: lightcoral;}
同级相邻选择器,选择拥有共同父级且相邻的元素
语法举例:li.red + li {color: lightcoral;}
同级所有选择器,选择拥有共同父级的后续所有元素
语法举例:li.red ~ li {color: lightcoral;}
我们重点放在伪类最重要的应用场景:
| 场景 | 描述 |
|---|---|
| 结构伪类 | 根据子元素的位置特征进行选择 |
| 表单伪类 | 根据表单控件状态特征进行选择 |
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 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) |
语法举例: .nva > :last-child {background-color: lightpink;}
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 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) |
语法举例: .nva span:last-of-type {background-color: violet;}
总结:css里面样式属性一些使用词比较多,应该要按规律背词,这样写前段的时候才能更顺畅
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号