---
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(/