--- id: bad87fee1348bd9aedf08805 title: Use CSS Selectors to Style Elements challengeType: 0 videoUrl: '' localeTitle: 使用CSS选择器设置样式元素 --- ## Description
使用CSS,您可以使用数百种CSS properties来更改元素在页面上的显示方式。当您输入<h2 style="color: red">CatPhotoApp</h2> ,您使用inline CSS (代表Cascading Style Sheets对单个h2元素进行Cascading Style Sheets 。这是指定元素样式的一种方法,但有一种更好的方法来应用CSS 。在代码的顶部,创建一个style块,如下所示:
<风格>
</样式>
在该样式块中,您可以为所有h2元素创建CSS selector 。例如,如果您希望所有h2元素都是红色,则可以添加如下所示的样式规则:
<风格>
h2 {color:red;}
</样式>
请注意,在每个元素的样式规则周围同时打开和关闭花括号( {} )非常重要。您还需要确保元素的样式定义位于开始和结束样式标记之间。最后,请务必在每个元素的样式规则的末尾添加分号。
## Instructions
删除h2元素的样式属性,而不是创建CSS style块。添加必要的CSS以将所有h2元素变为蓝色。
## Tests
```yml tests: - text: 从h2元素中删除style属性。 testString: 'assert(!$("h2").attr("style"), "Remove the style attribute from your h2 element.");' - text: 创建style元素。 testString: 'assert($("style") && $("style").length > 1, "Create a style element.");' - text: 你的h2元素应该是蓝色的。 testString: 'assert($("h2").css("color") === "rgb(0, 0, 255)", "Your h2 element should be blue.");' - text: 确保样式表h2声明对分号和右括号有效。 testString: 'assert(code.match(/h2\s*\{\s*color\s*:.*;\s*\}/g), "Ensure that your stylesheet h2 declaration is valid with a semicolon and closing brace.");' - text: 确保所有style元素都有效并具有结束标记。 testString: 'assert(code.match(/<\/style>/g) && code.match(/<\/style>/g).length === (code.match(//g) || []).length, "Make sure all your style elements are valid and have a closing tag.");' ```
## Challenge Seed
```html

CatPhotoApp

Click here to view more cat photos.

A cute orange cat lying on its back.

Things cats love:

  • cat nip
  • laser pointers
  • lasagna

Top 3 things cats hate:

  1. flea treatment
  2. thunder
  3. other cats


```
## Solution
```js // solution required ```