Skip to content

SASS 语法

变量

sass
$color: #fff;
body {
  color: $color;
}

嵌套

sass
.container {
  h1 {
    color: #fff;
  }
}

运算

sass
body {
  width: 100px + 50px;
}

继承

sass
%color-red {
  color: red;
}

.text {
  @extend %color-red;
}

// 输出:
.text {
  color: red;
}

混合

sass
@mixin border-radius($radius) {
  border-radius: $radius;
}

// 使用混合
.box {
  @include border-radius(10px);
}

插值

sass
$name: foo;
#{$name} {
  color: blue;
}

// 输出:
foo {
  color: blue;
}

函数

sass
@function double($n) {
  @return $n * 2;
}

#sidebar {
  width: double(5px); // 10px
}

条件语句

sass
@if lightness($color) >= 50% {
  background-color: #000;
} @else {
  background-color: #fff;
}

循环语句

sass
@for $i from 1 through 3 {
  .item-#{$i} { width: 2em * $i; }
}
// 输出:
.item-1 { width: 2em; }
.item-2 { width: 4em; }
.item-3 { width: 6em; }

最后更新于:

基于 MIT 许可发布