2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf04756
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: Class 选择器的优先级高于继承样式
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cGJDQug'
|
|
|
|
|
forumTopicId: 18253
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: override-styles-in-subsequent-css
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
"pink-text" class 覆盖了 `body` 元素的 CSS 声明。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
我们刚刚证明了我们的 class 会覆盖 `body` 的 CSS 样式。那么下一个问题是,我们要怎么样才能覆盖我们的 `pink-text` class 中所定义的样式?
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
创建一个字体颜色为 `blue` 的 `blue-text` class,并确保它在 `pink-text` 下方声明。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
在含有 `pink-text` class 的 `h1` 元素里面,添加上 `blue-text` class。我们来看看到底是谁获胜。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2019-12-13 13:47:57 +08:00
|
|
|
|
HTML 同时应用多个 class 属性需以空格来间隔,例子如下:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`class="class1 class2"`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
**注意:**HTML 元素里应用的 class 的先后顺序无关紧要。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
但是,在 `<style>` 标签里面声明的 `class` 顺序十分重要,之后的声明会覆盖之前的声明。由于 `.blue-text` 在 `.pink-text` 的后面出现,所以 `.blue-text` 里的样式会覆盖 `.pink-text` 里的样式。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素应包含 `pink-text` class。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('h1').hasClass('pink-text'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素应包含 `blue-text` class。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('h1').hasClass('blue-text'));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`blue-text` 和 `pink-text` 需同时应用于 `h1` 元素上。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.pink-text').hasClass('blue-text'));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素的颜色应为蓝色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('h1').css('color') === 'rgb(0, 0, 255)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 class="pink-text">Hello World!</h1>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 class="pink-text blue-text">Hello World!</h1>
|
|
|
|
|
```
|