批改状态:合格
老师批语:图片没上传成功,发布后打开看一下
| CSS 选择器 | 简述 |
|---|---|
| tag 选择器 | 标签名称 {},为相同标签设定统一的样式 |
| class 选择器 | .class 名称 {}, 为相同类名标签设定统一的样式 |
| id 选择器 | #id 名称 {},为相同 id 标签设定统一的样式 |

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>优先级相同</title><style>/* 0,0,3 */html body h1 {color: yellow;}/* 0,0,3 */html body h1 {color: red;}</style></head><body><h1>HelloWord</h1></body></html>

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>class选择器与tag选择器优先级</title><style>/* 0,0,3 */body h1.active {color: yellow;}/* 0,0,3 */body h1 {color: red;}</style></head><body><h1 class="active">HelloWord</h1></body></html>

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>id选择器与class选择器优先级</title><style>/* 0,1,2 */body h1#first {color: yellow;}/* 0,1,1 */body h1.active {color: red;}</style></style></head><body><h1 class="active" id="first">HelloWord</h1></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>行内样式表</title></head><body><header style="height: 4rem; background-color: #ddd">我是页眉</header><main style="height: 15rem; background-color: lightcyan">我是主体</main><footer style="height: 6rem; background-color: #999">我是页脚</footer></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>内部样式表</title><style>header {height: 4rem;background-color: #ddd;}main {height: 15rem;background-color: lightcyan;}footer {height: 6rem;background-color: #999;}</style></head><body><header>我是页眉</header><main>我是主体</main><footer>我是页脚</footer></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="stylesheet" href="css/mkh.css" /><title>外部样式表</title><style>/* @import url(css/mkh.css); */</style></head><body><header>我是页眉</header><main>我是主体</main><footer>我是页脚</footer></body></html>
header {height: 4rem;background-color: #ddd;}main {height: 15rem;background-color: lightcyan;}footer {height: 6rem;background-color: #999;}

公式:
n = (0,1,2,3,4…)
.list > li:nth-child(Xn + y)

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>结构伪类选择器</title><style>/* 设定ul标签内部第三个标签样式 */.list > :nth-child(3) {background-color: lightblue;}</style></head><body><ul class="list"><li>item1</li><p>item2</p><li>item3</li><p>item4</p><li>item5</li><p>item6</p><li>item7</li><p>item8</p><li>item9</li><p>item10</p></ul></body></html>

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>分组伪类结构选择器</title><style>/* 选择任何一个 :nth-of-type(n) *//* 选中第3个li */.list > li:nth-of-type(3) {background-color: red;}/* 选中第1个P */.list > p:nth-of-type(2) {background-color: yellow;}/* 选择第一个 :first-of-type *//* 选中第1个li */.list > li:first-of-type {background-color: brown;}/* 选择最后一个 :last-of-type *//* 选中第1个p */.list > p:last-of-type {background-color: violet;}/* 选择倒数某一个 :nth-last-of-type(n) *//* 选中倒数第2个li */.list > li:nth-last-of-type(2) {background-color: pink;}/* 选择唯一子元素的元素 :only-of-type *//* 选中唯一li的ul */ul > li:only-of-type {background-color: #ccc;}</style></head><body><ul class="list"><li>item1</li><p>item2</p><li>item3</li><p>item4</p><li>item5</li><p>item6</p><li>item7</li><p>item8</p><li>item9</li><p>item10</p></ul><ul><li>item</li></ul></body></html>
| 分组伪类结构选择器 | 说明 |
|---|---|
| :nth-of-type(n) | 选择任何一个 |
| :first-of-type | 选择第一个 |
| :last-of-type | 选择最后一个 |
| :nth-last-of-type(n) | 选择倒数某一个 |
| :only-of-type | 选择唯一子元素的元素 |
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号