2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf08756
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 样式中的优先级
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2019-12-13 05:47:57 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cZ8wnHv'
|
|
|
|
|
forumTopicId: 18258
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: prioritize-one-style-over-another
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
有时候,HTML 元素的样式会跟其他样式发生冲突。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
就像 `h1` 元素不能同时设置绿色和粉色两种颜色。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
让我们尝试创建一个字体颜色为粉色的 class,并应用于其中一个元素中。 猜一猜,它会 *覆盖* `body` 元素的 `color: green;` CSS 规则吗?
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
创建一个能将元素的字体颜色改为粉色的 class,并命名为 `pink-text`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
给 `h1` 元素添加 `pink-text` class。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`h1` 元素应含有 `pink-text` class。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('h1').hasClass('pink-text'));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`<style>` 标签应含有一个可以改变 `color` 的 `pink-text` class。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.pink-text\s*\{\s*color\s*:\s*.+\s*;\s*\}/g));
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`h1` 元素的字体颜色应为粉色。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert($('h1').css('color') === 'rgb(255, 192, 203)');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2019-12-13 05:47:57 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1>Hello World!</h1>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 class="pink-text">Hello World!</h1>
|
|
|
|
|
```
|