Scss常用到的一些函数及操作。
变量声明及使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $width: 1px; $width2: 2px; $pos: bottom;
.aa { width: $width; border-#{$pos}: 1px solid red; } .bb { width: $width + $width2; }
.aa { width: 1px; border-bottom: 1px solid red; } .bb { width: 3px; }
|
嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| div{ color: red; p { color: green; } &:hover{ color: blue; } }
.div{
border: { style: solid; left: { width: 4px; color: #888; } } }
|
颜色函数
1 2 3 4
| lighten(#cc3, 10%); darken(#cc3, 10%); grayscale(#cc3); complement(#cc3);
|
extend
继承目标选择器的所有样式(!optional防止空样式编译报错)
1 2 3 4 5 6
| .margin-top-10 { margin-top: 10px; } .container { @extend .margin-top-10!optional }
|
mixin
1 2 3 4 5 6
| @mixin margin-top-10 { margin-top: 10px; } .margin-top-10 { @include margin-top-10; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $margin: 10px; $border: none; @mixin aa($margin, $border) { margin: $margin; border: $border; } .bb { @include aa($margin, $border); }
$map: (left: 10px, border: none); @mixin cc($left, $border) { left: $left; border: $border; } .dd { @include cc($map...); }
@mixin ee($left: 10px) { left: $left; } .ff { @include ee(); }
|
function
1 2 3 4 5 6 7 8 9 10 11 12 13
| $width: 20px; @function fun($width) { @return $width * 2; } div { width: fun($width); }
div { width: 40px; }
|