--- id: bad87fee1348bd9aecf08806 title: Use a CSS Class to Style an Element challengeType: 0 videoUrl: '' localeTitle: 使用CSS类来设置元素的样式 --- ## Description
类是可重用的样式,可以添加到HTML元素中。这是一个CSS类声明示例:
<风格>
.blue-text {
颜色:蓝色;
}
</样式>
您可以看到我们在<style>标记内创建了一个名为blue-text的CSS类。您可以将类应用于HTML元素,如下所示: <h2 class="blue-text">CatPhotoApp</h2>请注意,在CSS style元素中,类名以句点开头。在HTML元素的class属性中,类名不包含句点。
## Instructions
style元素中,将h2选择器更改为.red-text并将颜色的值从blue更新为red 。为您的h2元素提供值为'red-text'class属性。
## Tests
```yml tests: - text: 你的h2元素应该是红色的。 testString: 'assert($("h2").css("color") === "rgb(255, 0, 0)", "Your h2 element should be red.");' - text: 你的h2元素应该有red-text类。 testString: 'assert($("h2").hasClass("red-text"), "Your h2 element should have the class red-text.");' - text: 样式表应声明一个red-text类,并将其颜色设置为红色。 testString: 'assert(code.match(/\.red-text\s*\{\s*color\s*:\s*red;\s*\}/g), "Your stylesheet should declare a red-text class and have its color set to red.");' - text: '不要在h2元素中使用style="color: red"内联样式声明。' testString: 'assert($("h2").attr("style") === undefined, "Do not use inline style declarations like style="color: red" in your h2 element.");' ```
## 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 ```