--- id: 587d7dbd367417b2b2512bb4 title: Store Data with Sass Variables challengeType: 0 videoUrl: '' localeTitle: 使用Sass变量存储数据 --- ## Description
Sass的一个与CSS不同的特性是它使用变量。它们被声明并设置为存储数据,类似于JavaScript。在JavaScript中,使用letconst关键字定义变量。在Sass中,变量以$开头,后跟变量名。以下是几个例子:
$ main-fonts:Arial,sans-serif;
$ headings-color:green;

//使用变量:
h1 {
font-family:$ main-fonts;
颜色:$ headings-color;
}
变量有用的一个例子是当许多元素需要是相同的颜色时。如果更改了该颜色,则编辑代码的唯一位置是变量值。
## Instructions
创建一个变量$text-color并将其设置为红色。然后将.blog-posth2color属性值更改为$text-color变量。
## Tests
```yml tests: - text: 您的代码应该具有为$text-color声明的Sass变量,其值为red。 testString: 'assert(code.match(/\$text-color:\s*?red;/g), "Your code should have a Sass variable declared for $text-color with a value of red.");' - text: 您的代码应使用$text-color变量来更改.blog-posth2项的color 。 testString: 'assert(code.match(/color:\s*?\$text-color;/g), "Your code should use the $text-color variable to change the color for the .blog-post and h2 items.");' - text: 您的.blog-post元素应该是红色。 testString: 'assert($(".blog-post").css("color") == "rgb(255, 0, 0)", "Your .blog-post element should have a color of red.");' - text: 你的h2元素应该是红色。 testString: 'assert($("h2").css("color") == "rgb(255, 0, 0)", "Your h2 elements should have a color of red.");' ```
## Challenge Seed
```html

Learn Sass

Some random title

This is a paragraph with some random text in it

Header #2

Here is some more random text.

Here is another header

Even more random text within a paragraph

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