s

scss语法

自定义变量

1
2
3
4
$color:pink;
.test1{
background-color:$color;
}

插入一个变量

1
2
3
4
$right:right;
.test2{
border-#{$right}:1px solid #000;
}

子元素书写

1
2
3
4
5
.text3{
.text33{
border:1px solid;
}
}

样式的加减乘除

1
2
3
4
5
6
$paramer:3;
.text4{
height:(1px+3px);
width: (96px/6);
right: $paramer*4;
}

继承

1
2
3
4
5
6
7
.class5{
border:1px solid red;
}
.class5E{
@extend .class5;
font-size:20px;
}

代码块的复用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@mixin text6 {
height:50px;
left:20px;
}
.text6M{
@include text6
}
//这里的mixin就是定义一个可以复用的代码段,当然我们也可以给它传递一个参数,就像这样一样:
@mixin text66($height){
height:$heigth;
left:20px;
}
.text6N{
@include text66(100px);
}

if语法,通过对if的判断来决定使用那一套样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.text7{
@if 1 +2 == 3 {
border:1px solid ;
}
@if 5 < 3 {
border:2px dsahed red;
}
}
//当然,我们都知道if一般是要和else配合的,所以我们也可以这样写
.test77{
@if lightness($color) > 30%{
background-color:#fff;
}@else{
background:#0ff;
}
}
//这里的lightness是一个scss颜色函数,$color指向之前定义的值。

循环语法,包括最常见的三种循环方法,for,while,each

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//for 循环
@for $i from 1 to 5 {
.item-#{$i} {
border:#{$i}px solid;
}
}
//while 循环
$m:8;
@while $m > 0 {
.items-#{$m} {
width:2em*$m;
}
$m:$m - 2 ;
}
//这里可以对$m进行运算 让它每次都减去2
//each 语法
@each $img in q,w,e,r {
.#{$img} {
background-image:url('#{$img}.png')
}
}

函数语法

1
2
3
4
5
6
@function double ($number){
@return $number*2;
}
.text9{
font-size:double(20px);
}

import导入语法

1
@import 'other.scss'