Cheat Sheet速查表
跳转到主要内容

Sass 速查表

本文根据 Devhints / Sass 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2020-07-03。

基础

简介

这是关于……的快速参考 Sass 样式表.

变量

$red: #833;
body {
  color: $red;
}

嵌套 (Nesting)

.markdown-body {
  a {
    color: blue;
    &:hover {
      color: red;
    }
  }
}

针对属性

text: {
  align: center;          // like text-align: center
  transform: uppercase;   // like text-transform: uppercase
}

注释

/* Block comments */
// Line comments

混合 (Mixins)

@mixin heading-font {
  font-family: sans-serif;
  font-weight: bold;
}
h1 {
  @include heading-font;
}

带参数

@mixin font-size($n) {
  font-size: $n * 1.2em;
}
body {
  @include font-size(2);
}

带默认值

@mixin pad($n: 10px) {
  padding: $n;
}
body {
  @include pad(15px);
}

带默认变量

// Set a default value
$default-padding: 10px;
@mixin pad($n: $default-padding) {
  padding: $n;
}
body {
  @include pad(15px);
}

扩展

.button {
  ···
}
.push-button {
  @extend .button;
}

组合

@import './other_sass_file';
@use './other_sass_file';

该 @import 不建议使用此规则,因为它最终会被 从语言中移除.
相反,我们应该使用 @use 规则.
该 .scss 或 .sass 扩展名是可选的。

颜色函数

rgba

rgb(100, 120, 140)
rgba(100, 120, 140, .5)
rgba($color, .5)

混合

mix($a, $b, 10%)   // 10% a, 90% b

修改 HSLA

darken($color, 5%)
lighten($color, 5%)
saturate($color, 5%)
desaturate($color, 5%)
grayscale($color)
adjust-hue($color, 15deg)
complement($color)    // like adjust-hue(_, 180deg)
invert($color)
fade-in($color, .5)   // aka opacify()
fade-out($color, .5)  // aka transparentize() - halves the opacity
rgba($color, .5)      // sets alpha to .5

获取单个数值

HSLA

hue($color)         // → 0deg..360deg
saturation($color)  // → 0%..100%
lightness($color)   // → 0%..100%
alpha($color)       // → 0..1 (aka opacity())

RGB

red($color)         // → 0..255
green($color)
blue($color)

参见: hue(), red()

调整

// Changes by fixed amounts
adjust-color($color, $blue: 5)
adjust-color($color, $lightness: -30%)   // like darken(_, 30%)
adjust-color($color, $alpha: -0.4)       // like fade-out(_, .4)
adjust-color($color, $hue: 30deg)        // like adjust-hue(_, 15deg)
// Changes via percentage
scale-color($color, $lightness: 50%)
// Changes one property completely
change-color($color, $hue: 180deg)
change-color($color, $blue: 250)

支持: $red $green $blue $hue $saturation $lightness $alpha

其他函数

字符串

unquote('hello')
quote(hello)
to-upper-case(hello)
to-lower-case(hello)
str-length(hello world)
str-slice(hello, 2, 5)      // "ello" - it's 1-based, not 0-based
str-insert("abcd", "X", 1)  // "Xabcd"

单位

unit(3em)        // 'em'
unitless(100px)  // false

数字

floor(3.5)
ceil(3.5)
round(3.5)
abs(3.5)
min(1, 2, 3)
max(1, 2, 3)
percentage(.5)   // 50%
random(3)        // 0..3

杂项

variable-exists(red)    // checks for $red
mixin-exists(red-text)  // checks for @mixin red-text
function-exists(redify)
global-variable-exists(red)
selector-append('.menu', 'li', 'a')   // .menu li a
selector-nest('.menu', '&:hover li')  // .menu:hover li
selector-extend(...)
selector-parse(...)
selector-replace(...)
selector-unify(...)

特性检测

功能检查

feature-exists(global-variable-shadowing)

特性

  • 全局变量遮蔽 (global-variable-shadowing)
  • 扩展选择器伪类 (extend-selector-pseudoclass)
  • Level 3 单位 (units-level-3)
  • @error 指令

循环

For 循环

@for $i from 1 through 4 {
  .item-#{$i} { left: 20px * $i; }
}

Each 循环(简单)

$menu-items: home about services contact;

@each $item in $menu-items {
  .photo-#{$item} {
    background: url('images/#{$item}.jpg');
  }
}

Each 循环(嵌套)

$backgrounds: (home, 'home.jpg'), (about, 'about.jpg');

@each $id, $image in $backgrounds {
  .photo-#{$id} {
    background: url($image);
  }
}

While 循环

$i: 6;
@while $i > 0 {
  .item-#{$i} { width: 2em * $i; }
  $i: $i - 2;
}

其他功能

条件判断

@if $position == 'left' {
   position: absolute;
   left: 0;
}
@else if $position == 'right' {
   position: absolute;
   right: 0;
}
@else {
   position: static;
}

插值

.#{$klass} { ... }      // Class
call($function-name)    // Functions

@media #{$tablet}
font: #{$size}/#{$line-height}
url("#{$background}.jpg")

列表 (Lists)

$list: (a b c);

nth($list, 1)  // starts with 1
length($list)

@each $item in $list { ... }

映射 (Maps)

$map: (key1: value1, key2: value2, key3: value3);

map-get($map, key1)

另请参阅