批改状态:合格
老师批语:看来你对伪元素是掌握了, 恭喜
| data | desc |
|---|---|
| 元素(标签)选择器 | 直接使用元素(标签)名称 |
| class(类)选择器 | 使用.加上 class(类)名称 |
| id 选择器 | 使用#加上 id 名称 |
| 复合选择器 | 使用多个类或类和 id 等组合,中间不能有空格 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>简单选择器</title><style>/* 使用九宫格来演示: grid布局实现 */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* 元素(标签)选择器 */body {background-color: lightgrey; /*背景颜色*/}/* class(类)选择器 */.item {border: 1px dashed #f00; /*1px虚线红色边框*/}/* id选择器 */#frist {background-color: lightsalmon; /*背景颜色*/}/* 多个类复合选择器 */.item.center {background-color: lightpink; /*背景颜色*/}</style></head><body><div class="container"><div class="item" id="frist">01</div><div class="item">02</div><div class="item">03</div><div class="item">04</div><div class="item center">05</div><div class="item">06</div><div class="item">07</div><div class="item">08</div><div class="item">09</div></div></body></html>

| data | desc |
|---|---|
| 后代选择器 | 用空格连接选择器 |
| 父子选择器 | 用>连接选择器 |
| 同级相邻选择器 | 用+连接选择器 |
| 同级所有选择器 | 用~连接选择器 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>上下文选择器</title><style>/* 使用九宫格来演示: grid布局实现 */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* 后代选择器 */.container div {border: 1px dashed #f00; /*1px虚线红色边框*/}/* 父子选择器 */body > div {background-color: #ff0; /*背景颜色*/padding: 5px; /*内边距*/}/* 同级相邻选择器 */.center + .item {background-color: lightpink;}/* 同级所有选择器 */.center ~ .item {color: #fff; /*字体颜色*/}</style></head><body><div class="container"><div class="item">01</div><div class="item">02</div><div class="item">03</div><div class="item">04</div><div class="item center">05</div><div class="item">06</div><div class="item">07</div><div class="item">08</div><div class="item">09</div></div></body></html>

| data | desc |
|---|---|
| 匹配第 1 个元素 | :first-child |
| 匹配最后 1 个元素 | :last-child |
| 匹配第 N 个元素 | :nth-child(N)或:nth-last-child(N) |
| 匹配所有的偶数元素 | :nth-child(even)或:nth-child(2n) |
| 匹配所有的奇数元素 | :nth-child(odd)或:nth-child(2n-1) |
| 匹配第 N 个位置开始的所有元素 | :nth-child(n + N) |
| 匹配前 N 个元素 | :nth-child(-n + N) |
| 匹配后 N 个元素 | :nth-last-child(-n + 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>/* 使用九宫格来演示: grid布局实现 */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* 匹配第1个子元素 */.container > :first-child {background-color: lightpink; /*背景色*/}/* 匹配最后1个子元素 */.container > :last-child {background-color: lightcoral; /*背景色*/}/* 匹配第5个子元素-1 */.container > :nth-child(5) {background-color: lightslategray; /*背景色*/}/* 匹配第5个子元素-2 */.container > :nth-last-child(5) {border-radius: 50px; /*设置圆角边框*/}/* 匹配所有的偶数子元素-1 */.container > :nth-child(2n) {color: blue; /*字体颜色*/}/* 匹配所有的偶数子元素-2 */.container > :nth-child(even) {font-size: 3rem; /*字体大小*/}/* 匹配所有的奇数子元素-1 */.container > :nth-child(2n-1) {color: yellow; /*字体颜色*/}/* 匹配所有的奇数子元素-2 */.container > :nth-child(odd) {font-size: 3rem; /*字体大小*/}/* 匹配指定位置(5)开始所有的子元素 */.container > :nth-child(n + 5) {background-color: black; /*背景色*/}/* 匹配前4个子元素 */.container > :nth-child(-n + 4) {border-radius: 50px; /*设置圆角边框*/}/* 匹配最后4个子元素 */.container > :nth-last-child(-n + 4) {border-radius: 50px; /*设置圆角边框*/}</style></head><body><div class="container"><div class="item">01</div><div class="item">02</div><div class="item">03</div><div class="item">04</div><div class="item">05</div><div class="item">06</div><div class="item">07</div><div class="item">08</div><div class="item">09</div></div></body></html>

| data | desc |
|---|---|
| 匹配分组中第 1 个元素 | :first-of-type |
| 匹配分组中最后 1 个元素 | :last-of-type |
| 匹配分组匹配第 N 个元素 | :nth-of-type(N) |
| 匹配分组中前 N 个元素 | :nth-of-type(-n + N) |
| 匹配分组 z 中后 N 个元素 | :nth-last-of-type(-n + 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>/* 使用九宫格来演示: grid布局实现 */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* span分组中匹配的最后一个元素 */.container span:last-of-type {background-color: brown; /*背景色*/}/* div分组中匹配的第一个元素 */.container div:first-of-type {background-color: lightpink; /*背景色*/}/* span分组中匹配第3个元素 */.container span:nth-of-type(3) {background-color: chocolate; /*背景色*/}/* span分组中匹配前3个元素 */.container span:nth-of-type(-n + 3) {color: white; /*字体颜色*/}/* span分组中匹配最后3个元素 */.container span:nth-last-of-type(-n + 3) {color: white; /*字体颜色*/}</style></head><body><div class="container"><div class="item">01</div><div class="item">02</div><div class="item">03</div><span class="item">04</span><span class="item">05</span><span class="item">06</span><span class="item">07</span><span class="item">08</span><span class="item">09</span></div></body></html>

| data | desc |
|---|---|
| 锚点激活的时候触发 | :target |
| 获取焦点时触发 | :focus |
| 选中文本时触发 | ::selection |
| 选择不满足条件的元素 | :not() |
| 在某元素前面生成 | ::before |
| 在某元素后面生成 | ::before |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>伪类与伪元素</title><style>#login-form {display: none; /*隐藏元素*/}/* 当前#login-form表单元素被a标签的锚点激活的时候触发 */#login-form:target {display: block; /*显示元素*/}/* 获取焦点的时候 */input:focus {background-color: lightskyblue; /*背景色*/}/* 设置选中文本的前景色和背景色 */::selection {color: white; /*字体颜色*/background-color: maroon; /*背景色*/}/* 选择不满足条件的元素 */ul > :not(:nth-of-type(3)) {color: red; /*字体颜色*/}/* 在ul前面生成 */ul::before {content: "ul前面添加的内容"; /*内容*/color: blue; /*字体颜色*/font-size: 1.5rem; /*字体大小*/}/* 在ul后面生成 */ul::after {content: "ul后面添加的内容"; /*内容*/color: yellow; /*字体颜色*/font-size: 1.2rem; /*字体大小*/}</style></head><body><!-- <a href="#login-form">我要登录</a> --><button onclick="location='#login-form'">点击登录</button><form action="" method="POST" id="login-form"><label for="email">邮箱:</label><input type="email" name="email" id="email" /><label for="password">密码:</label><input type="password" name="password" id="password" /><button>登录</button></form><hr /><ul><li>01</li><li>02</li><li>03</li><li>04</li></ul></body></html>
:nth-child(N)或:nth-last-child(N)等选择第 N 个元素时,css 索引是从 1 开始的:,伪元素前面是 2 个:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号