3.5 KiB
3.5 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7dbd367417b2b2512bb6 | Create Reusable CSS with Mixins | 0 | 使用Mixins创建可重用的CSS |
Description
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
tests:
- text: 你的代码应该声明一个名为<code>border-radius</code>的<code>mixin</code> ,它有一个名为<code>$radius</code>的参数。
testString: assert(code.match(/@mixin\s+?border-radius\s*?\(\s*?\$radius\s*?\)\s*?{/gi));
- text: 您的代码应包含使用<code>$radius</code>参数的<code>-webkit-border-radius</code> vender前缀。
testString: assert(code.match(/-webkit-border-radius:\s*?\$radius;/gi));
- text: 您的代码应包含使用<code>$radius</code>参数的<code>-moz-border-radius</code> vender前缀。
testString: assert(code.match(/-moz-border-radius:\s*?\$radius;/gi));
- text: 您的代码应包含使用<code>$radius</code>参数的<code>-ms-border-radius</code> vender前缀。
testString: assert(code.match(/-ms-border-radius:\s*?\$radius;/gi));
- text: 您的代码应包含使用<code>$radius</code>参数的一般<code>border-radius</code>规则。
testString: assert(code.match(/border-radius:\s*?\$radius;/gi).length == 4);
- text: 您的代码应使用<code>@include</code>关键字调用<code>border-radius mixin</code> ,将其设置为15px。
testString: assert(code.match(/@include\s+?border-radius\(\s*?15px\s*?\);/gi));
Challenge Seed
<style type='text/sass'>
#awesome {
width: 150px;
height: 150px;
background-color: green;
}
</style>
<div id="awesome"></div>
Solution
// solution required