.login-error { //@include error() is ok also; @include error; } .logout-error { @include error(10px); }
selector nesting
nested selector is child of parent, when converted to css, a space is added between parent and child but for pesudo class and pesudo element they must no space, hence in these two case, we should use & to refer its parent directly.
//converted to css section { margin: 10px; } //a space between section and nav selector sectionnav { height: 25px; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
section { margin: 10px; nav { height: 25px; a { color: #0982c1; /* & 引用父选择器 section nav a with &: section nav a:hover without&: section nav a :hover -->not working for hover due to extra space. */ &:hover { text-decoration: underline; } } } }
Inheritance
extend to inherits all properties from its parent
1 2 3 4 5 6 7 8 9 10
%block_base { margin: 10px5px; padding: 2px; }
p { //extend base class @extend %block_base border: 1px solid #eee; }
$type: monster; p { @if$type == ocean { color: blue; } @else if $type == matador { color: red; } @else if $type == monster { color: green; } @else { color: black; } } //note: number and string use same operator "==" and no () for condition;
/* include 3, 1, 2, 3 */ /* @for..from..through to iterate a range of number */ @for$i from 1 through 2 { .item-#{$i} { width: 2em * $i; } } //converted to css as you can see #{$i} used to link string and variable!!! .item-1 { width: 2em; }
.item-2 { width: 4em; }
//@each ...in bot work for list and map @each$var in <list>{}
list items can be seperated by space, comma list functions
length($list)
join($list1,$list2,[$separator]) return a new list, the orignal unchanged
append($list,$value,[$separator]), return a new list, the orignal unchanged
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//define a list with different separators(can only be comma or space!!!) $linkColor: #08c#333; //第一个值为默认值,第二个鼠标滑过值 $linkColor: #08c,#333; //第一个值为默认值,第二个鼠标滑过值 a { color: nth($linkColor, 1); &:hover { color: nth($linkColor, 2); } //append a new element and assign to itself, no quote for green $linkColor: append($linkColor, green); button { color: nth($linkColor, 3); } }