2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08736
|
2021-01-08 11:20:48 -08:00
|
|
|
title: 给 HTML 的 body 元素添加样式
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cB77PHW'
|
|
|
|
forumTopicId: 18313
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: style-the-html-body-element
|
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
|
|
|
现在让我们来讨论一下 CSS 中的继承。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
每一个 HTML 页面都有一个 `body` 元素。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
我们可以通过设置 `background-color` 的属性值为 黑色,来证明 `body` 元素的存在。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
请将以下代码添加到 `style` 标签里面:
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
body {
|
|
|
|
background-color: black;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
`body` 元素的 `background-color` 应为黑色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('body').css('background-color') === 'rgb(0, 0, 0)');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
确保 CSS 规则格式书写正确,左右大括号也应匹配。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
code.match(/<style>\s*body\s*\{\s*background.*\s*:\s*.*;\s*\}\s*<\/style>/i)
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
确保 CSS 规则以分号结尾。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
code.match(/<style>\s*body\s*\{\s*background.*\s*:\s*.*;\s*\}\s*<\/style>/i)
|
|
|
|
);
|
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>
|
|
|
|
|
|
|
|
</style>
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
background-color: black;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
```
|