---
id: 587d7dbd367417b2b2512bb6
title: Create Reusable CSS with Mixins
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.10.9/sass.sync.min.js'
raw: true
challengeType: 0
videoUrl: ''
localeTitle: 使用Mixins创建可重用的CSS
---
## Description
在Sass中, mixin
是一组CSS声明,可以在整个样式表中重用。较新的CSS功能需要一段时间才能完全采用并准备好在所有浏览器中使用。随着功能添加到浏览器中,使用它们的CSS规则可能需要供应商前缀。考虑“盒子阴影”: div {
-webkit-box-shadow:0px 0px 4px #fff;
-moz-box-shadow:0px 0px 4px #fff;
-ms-box-shadow:0px 0px 4px #fff;
box-shadow:0px 0px 4px #fff;
}
对于具有box-shadow
所有元素重写此规则,或者更改每个值以测试不同的效果,需要进行大量的输入。 Mixins
就像CSS的功能。这是如何写一个: @mixin box-shadow($ x,$ y,$ blur,$ c){
-webkit-box-shadow:$ x,$ y,$ blur,$ c;
-moz-box-shadow:$ x,$ y,$ blur,$ c;
-ms-box-shadow:$ x,$ y,$ blur,$ c;
box-shadow:$ x,$ y,$ blur,$ c;
}
该定义以@mixin
开头,后跟自定义名称。参数(上例中的$x
, $y
, $blur
和$c
)是可选的。现在,只要需要一个box-shadow
规则,只需要调用mixin
一行代替必须输入所有供应商前缀。使用@include
指令调用mixin
: div {
@include box-shadow(0px,0px,4px,#fff);
}
## Instructions
为border-radius
写一个mixin
并给它一个$radius
参数。它应该使用示例中的所有供应商前缀。然后使用border-radius
mixin
为#awesome
元素提供15px的边界半径。
## Tests
```yml
tests:
- text: 你的代码应该声明一个名为border-radius
的mixin
,它有一个名为$radius
的参数。
testString: 'assert(code.match(/@mixin\s+?border-radius\s*?\(\s*?\$radius\s*?\)\s*?{/gi), "Your code should declare a mixin
named border-radius
which has a parameter named $radius
.");'
- text: 您的代码应包含使用$radius
参数的-webkit-border-radius
vender前缀。
testString: 'assert(code.match(/-webkit-border-radius:\s*?\$radius;/gi), "Your code should include the -webkit-border-radius
vender prefix that uses the $radius
parameter.");'
- text: 您的代码应包含使用$radius
参数的-moz-border-radius
vender前缀。
testString: 'assert(code.match(/-moz-border-radius:\s*?\$radius;/gi), "Your code should include the -moz-border-radius
vender prefix that uses the $radius
parameter.");'
- text: 您的代码应包含使用$radius
参数的-ms-border-radius
vender前缀。
testString: 'assert(code.match(/-ms-border-radius:\s*?\$radius;/gi), "Your code should include the -ms-border-radius
vender prefix that uses the $radius
parameter.");'
- text: 您的代码应包含使用$radius
参数的一般border-radius
规则。
testString: 'assert(code.match(/border-radius:\s*?\$radius;/gi).length == 4, "Your code should include the general border-radius
rule that uses the $radius
parameter.");'
- text: 您的代码应使用@include
关键字调用border-radius mixin
,将其设置为15px。
testString: 'assert(code.match(/@include\s+?border-radius\(\s*?15px\s*?\);/gi), "Your code should call the border-radius mixin
using the @include
keyword, setting it to 15px.");'
```
## Challenge Seed
## Solution
```js
// solution required
```