把每个点最简单的部分记录一下,方便自己查找
官方文档链接
&
父选择器,编译后为父选择器a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}
编译为
a {
font-weight: bold;
text-decoration: none; }
a:hover {
text-decoration: underline; }
body.firefox a {
font-weight: normal; }
:
嵌套属性.funky {
font: 20px/24px {
family: fantasy;
weight: bold;
}
}
编译为
.funky {
font: 20px/24px;
font-family: fantasy;
font-weight: bold; }
$
变量#main {
$width: 5em !global; // !global:声明为全局变量,可在作用域外使用
width: $width;
}
#sidebar {
width: $width;
}
编译为
#main {
width: 5em;
}
#sidebar {
width: 5em;
}
1, 2, 13, 10px
"foo", 'bar', baz
blue, #04a3f9, rgba(255,0,0,0.5)
true, false
null
1.5em 1em 0 2em, Helvetica, Arial, sans-serif
(key1: value1, key2: value2)
$i: 6;
$i: $i - 2;
$name: jack;
数组之间的元素可以用,
隔开,也可以不用
margin: 10px 15px 0 0
font-face: Helvetica, Arial, sans-serif
nth
函数可以直接访问数组中的某一项;
join
函数可以将多个数组连接在一起;
append
函数可以在数组中添加新值;
@each
指令能够遍历数组中的每一项。
(key1 : value1,key2 : value2)
+
-
*
/
>
<
>
=
<=
==
!=
/
有两个作用:除法,分隔数字,具体怎么用看文档+
也用作字符串连接,计算结果以+
左侧的字符串为准+ 左侧 |
+ 右侧 |
连接后 |
---|---|---|
有引号 | 有引号 | 有引号 |
无引号 | 无引号 | 无引号 |
有引号 | 无引号 | 有引号 |
无引号 | 有引号 | 无引号 |
#{}
插值语法$name: foo;
$attr: border;
p.#{$name} {
#{$attr}-color: blue;
}
编译为
p.foo {
border-color: blue; }
@mixin firefox-message($selector) {
body.firefox #{$selector}:before {
content: "Hi, Firefox users!";
}
}
@include firefox-message(".header");
编译为
body.firefox .header:before {
content: "Hi, Firefox users!"; }
@import ‘foo’
@import url(../xxxx/xxx.css)
属性重复,谁在后面执行谁有优先权
.error {
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
@extend .error;
border-width: 3px;
}
编译后
.error .seriousError{
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
border-width: 3px;
}
@if
@else if
@else
$type: monster;
p {
@if $type == ocean {
color: blue;
} @else if $type == matador {
color: red;
} @else if $type == monster {
color: green;
} @else {
color: black;
}
}
编译后
p{
color: green
}
@for
@for $var from
包含start和end的值
@for $var from
不包含start的值,包含end的值
@for $i from 1through 3 {
.item-#{$i} { width : 2em * $i }
}
编译后
.item-1 { width: 2em }
.item-2 { width: 4em }
.itme-3 { width: 6em }
@each
@each $var in
list可以是一连串的字符串、数组、map
@each $animal in puma, sea-slug, etret, salamander {
.#{$animal}-icon{
background-image: url('/images/#{$animal}.png');
}
}
编译后
.puma-icon { background-image: url('/images/puma.png'); }
.sea-slug-icon { background-image: url('/images/sea-slug-icon.png'); }
.etret-icon { background-image: url('/images/etret.png'); }
.salamander-icon { background-image: url('/images/salamander.png'); }
map
@each $header, $size in (h1: 2em, h2: 1.5em, h3: 1.2em) {
#{$header} {
font-size: $size;
}
}
编译后
h1 {
font-size: 2em; }
h2 {
font-size: 1.5em; }
h3 {
font-size: 1.2em; }
@each $animal, $color, $cursor in (puma, black, default),
(sea-slug, blue, pointer),
(egret, white, move) {
.#{$animal}-icon {
background-image: url('/images/#{$animal}.png');
border: 2px solid $color;
cursor: $cursor;
}
}
编译后
.puma-icon {
background-image: url('/images/puma.png');
border: 2px solid black;
cursor: default; }
.sea-slug-icon {
background-image: url('/images/sea-slug.png');
border: 2px solid blue;
cursor: pointer; }
.egret-icon {
background-image: url('/images/egret.png');
border: 2px solid white;
cursor: move; }
@while
$i: 6;
@while $i > 0 {
.item-#{$i} { width: 2em * $i; }
$i: $i - 2;
}
编译后
.item-6 {
width: 12em; }
.item-4 {
width: 8em; }
.item-2 {
width: 4em; }
@mixin
像定义只是存放数据的函数,但是必须用@include
调用
@mixin large-text {
font: {
family: Arial;
size: 20px;
weight: bold;
}
color: #ff0000;
}
.page-title {
@include large-text;.
padding: 4px;
margin-top: 10px;
}
.page-title {
font-family: Arial;
font-size: 20px;
font-weight: bold;
color: #ff0000;
padding: 4px;
margin-top: 10px; }
@mixin silly-links {
a {
color: blue;
background-color: red;
}
}
@include silly-links;
编译后
a {
color: blue;
background-color: red;
}
@mixin sexy-border($color, $width: 1in) {
border: {
color: $color;
width: $width;
style: dashed;
}
}
p { @include sexy-border(blue); }
或者
p { @include sexy-border($color: blue); }
编译后
p{
border-color: blue;
border-width: 1in;
border-style: dashed;
}
@function
函数指令 @return
返回$grid-width: 40px;
$gutter-width: 10px;
@function grid-width($n) {
@return $n * $grid-width + ($n - 1) * $gutter-width;
}
#sidebar { width: grid-width(5); }
编译为
#sidebar {
width: 240px; }