Sass
这是一份快速参考备忘单,列出了 SASS 最有用的功能
Sass 基础
介绍
Sass 是一种 CSS 的预编译语言
BASH
1 | $ npm install -g sass |
在 Node.js 环境中使用 Sass
BASH
1 | $ sass source/index.scss build/index.css |
变量
SCSS
1 | $defaultLinkColor: #46EAC2; |
字符串插值
SCSS
1 | $wk: -webkit-; |
注释
SCSS
1 | /* |
Extend
SCSS
1 | .button { |
嵌套(Nesting)
SCSS
1 | nav { |
编译 css 为:
SCSS
1 | nav ul { |
模块(片段)
SCSS
1 | // _base.scss |
注意以下划线开头的 Sass 文件
SCSS
1 | // styles.scss |
编译 css 为:
CSS
1 | .inverse { |
混合(Mixins)
SCSS
1 | @mixin heading-font { |
查看: 混合(Mixins)
@import
SCSS
1 | @import './other_sass_file'; |
.sass 或 .sass 扩展名是可选的。
Sass 混合(Mixins)
参数
SCSS
1 | @mixin font-size($n) { |
SCSS
1 | body { |
默认值
SCSS
1 | @mixin pad($n: 10px) { |
SCSS
1 | body { |
默认变量
SCSS
1 | $default-padding: 10px; |
Sass 颜色函数
rgba
SCSS
1 | rgb(100, 120, 140) |
Mixing
SCSS
1 | mix($a, $b, 10%) // 10% a, 90% b |
修改 HSLA
SCSS
1 | darken($color, 5%) |
SCSS
1 | saturate($color, 5%) |
SCSS
1 | adjust-hue($color, 15deg) |
SCSS
1 | fade-in($color, .5) // aka opacify() |
获取值
HSLA
SCSS
1 | hue($color) // 0deg..360deg |
RGB
SCSS
1 | red($color) // 0..255 |
| :- | :- |
|---|---|
color.red() |
用于获取颜色的红色通道 |
color.green() |
用于获得颜色的绿色通道 |
color.blue() |
用于获取颜色的蓝色通道 |
color.hue() |
以获得颜色的色调 |
color.saturation() |
用于获得颜色的饱和度 |
color.lightness() |
以获得颜色的亮度 |
Sass 内置了对颜色值的支持
SCSS
1 | @debug rgb(204, 102, 153); // #c69 |
调整
SCSS
1 | // 固定金额变动 |
支持的: $red, $green, $blue, $hue, $saturation, $lightness, $alpha
Sass 其他函数
字符串
SCSS
1 | unquote('hello') |
SCSS
1 | to-upper-case(hello) |
SCSS
1 | str-length(hello world) |
Numbers
SCSS
1 | floor(4.2) // 4 |
SCSS
1 | min(1px, 4px) // 1px |
SCSS
1 | percentage(.5) // 50% |
Units
SCSS
1 | unit(3em) // 'em' |
Units
SCSS
1 | unit(3em) // 'em' |
Misc
SCSS
1 | // 检查 $red |
SCSS
1 | global-variable-exists(red) |
SCSS
1 | // .menu li a |
Sass 功能检查
功能检查
SCSS
1 | meta.feature-exists($feature) |
SCSS
1 | @mixin debug-content-exists { |
功能
| :- | :- |
|---|---|
global-variable-shadowing # |
这意味着局部变量将隐藏全局变量,除非它具有 !global 标志 |
extend-selector-pseudoclass # |
这意味着 @extend 规则将影响嵌套在伪类中的选择器,如 :not() |
units-level-3 # |
这意味着单位算术支持在 CSS 值和单位级别 3 中定义的单位 |
at-error # |
这意味着支持 @error 规则 |
custom-property # |
这意味着自定义属性声明值不支持除插值之外的任何表达式 |
Sass 循环
For 循环
SCSS
1 | $base-color: #036; |
编译 css 为:
CSS
1 | ul:nth-child(3n + 1) { |
Each 循环(简单)
SCSS
1 | $sizes: 40px, 50px; |
编译 css 为:
CSS
1 | .icon-40px { |
Each 循环(嵌套)
SCSS
1 | $icons: ("eye": "\f112", "start": "\f12e"); |
编译 css 为:
CSS
1 | .icon-eye:before { |
While 循环
SCSS
1 | @use "sass:math"; |
编译 css 为:
CSS
1 | sup { |
Sass 其它功能
条件句
SCSS
1 | @mixin avatar($size, $circle: false) { |
编译 css 为:
CSS
1 | .square-av { |
插值
SCSS
1 | .#{$klass} { ... } // Class |
列表
SCSS
1 | $list: (a b c); |
Maps
SCSS
1 | $map: (key1: value1, key2: value2, key3: value3); |