Sass语法

简介

Sass 是一款强化 CSS 的辅助工具,它在 CSS 语法的基础上增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,这些拓展令 CSS 更加强大与优雅。使用 Sass 以及 Sass 的样式库(如 Compass)有助于更好地组织管理样式文件,以及更高效地开发项目。

1. 特色功能 (Features)

  • 完全兼容 CSS3
  • 在 CSS 基础上增加变量、嵌套 (nesting)、混合 (mixins) 等功能
  • 通过函数进行颜色值与属性值的运算
  • 提供控制指令 (control directives)等高级功能
  • 自定义输出格式

嵌套规则 (Nested Rules)

Sass 允许将一套 CSS 样式嵌套进另一套样式中,内层的样式将它外层的选择器作为父选择器,例如:

1
2
3
4
5
6
7
8
9
#main p {
color: #00ff00;
width: 97%;

.redbox {
background-color: #ff0000;
color: #000000;
}
}

编译为

1
2
3
4
5
6
#main p {
color: #00ff00;
width: 97%; }
#main p .redbox {
background-color: #ff0000;
color: #000000; }

嵌套功能避免了重复输入父选择器,而且令复杂的 CSS 结构更易于管理:

1
2
3
4
5
6
7
8
9
10
#main {
width: 97%;

p, div {
font-size: 2em;
a { font-weight: bold; }
}

pre { font-size: 3em; }
}

编译为

1
2
3
4
5
6
7
8
#main {
width: 97%; }
#main p, #main div {
font-size: 2em; }
#main p a, #main div a {
font-weight: bold; }
#main pre {
font-size: 3em; }

父选择器

在嵌套 CSS 规则时,有时也需要直接使用嵌套外层的父选择器,例如,当给某个元素设定 hover 样式时,或者当 body 元素有某个 classname 时,可以用 & 代表嵌套规则外层的父选择器。

1
2
3
4
5
6
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}

编译为

1
2
3
4
5
6
7
a {
font-weight: bold;
text-decoration: none; }
a:hover {
text-decoration: underline; }
body.firefox a {
font-weight: normal; }

编译后的 CSS 文件中 & 将被替换成嵌套外层的父选择器,如果含有多层嵌套,最外层的父选择器会一层一层向下传递:

属性嵌套

有些 CSS 属性遵循相同的命名空间 (namespace),比如 font-family, font-size, font-weight 都以 font 作为属性的命名空间。为了便于管理这样的属性,同时也为了避免了重复输入,Sass 允许将属性嵌套在命名空间中,例如:

1
2
3
4
5
6
7
.funky {
font: {
family: fantasy;
size: 30em;
weight: bold;
}
}

编译为

1
2
3
4
.funky {
font-family: fantasy;
font-size: 30em;
font-weight: bold; }

命名空间也可以包含自己的属性值,例如:

1
2
3
4
5
6
.funky {
font: 20px/24px {
family: fantasy;
weight: bold;
}
}

编译为

1
2
3
4
.funky {
font: 20px/24px;
font-family: fantasy;
font-weight: bold; }

注释 /* *///

Sass 支持标准的 CSS 多行注释 /* */,以及单行注释 //,前者会 被完整输出到编译后的 CSS 文件中,而后者则不会,例如:

1
2
3
4
5
6
7
8
9
10
/* This comment is
* several lines long.
* since it uses the CSS comment syntax,
* it will appear in the CSS output. */
body { color: black; }

// These comments are only one line long each.
// They won't appear in the CSS output,
// since they use the single-line comment syntax.
a { color: green; }

编译为

1
2
3
4
5
6
7
8
9
/* This comment is
* several lines long.
* since it uses the CSS comment syntax,
* it will appear in the CSS output. */
body {
color: black; }

a {
color: green; }

变量 $

SassScript 最普遍的用法就是变量,变量以美元符号开头,赋值方法与 CSS 属性的写法一样:

1
$width: 5em;

直接使用即调用变量:

1
2
3
#main {
width: $width;
}

数字运算 (Number Operations)

SassScript 支持数字的加减乘除、取整等运算 (+, -, *, /, %),如果必要会在不同单位间转换值。

1
2
3
p {
width: 1in + 8pt;
}

编译为

1
2
p {
width: 1.111in; }

关系运算 <, >, <=, >= 也可用于数字运算,相等运算 ==, != 可用于所有数据类型。

插值语句 #{} (Interpolation: #{})

通过 #{} 插值语句可以在选择器或属性名中使用变量:

1
2
3
4
5
$name: foo;
$attr: border;
p.#{$name} {
#{$attr}-color: blue;
}

编译为

1
2
p.foo {
border-color: blue; }

#{} 插值语句也可以在属性值中插入 SassScript,大多数情况下,这样可能还不如使用变量方便,但是使用 #{} 可以避免 Sass 运行运算表达式,直接编译 CSS。

1
2
3
4
5
p {
$font-size: 12px;
$line-height: 30px;
font: #{$font-size}/#{$line-height};
}

编译为

1
2
p {
font: 12px/30px; }

@media

Sass 中 @media 指令与 CSS 中用法一样,只是增加了一点额外的功能:允许其在 CSS 规则中嵌套。如果 @media 嵌套在 CSS 规则内,编译时,@media 将被编译到文件的最外层,包含嵌套的父选择器。这个功能让 @media 用起来更方便,不需要重复使用选择器,也不会打乱 CSS 的书写流程。

1
2
3
4
5
6
.sidebar {
width: 300px;
@media screen and (orientation: landscape) {
width: 500px;
}
}

编译为

1
2
3
4
5
.sidebar {
width: 300px; }
@media screen and (orientation: landscape) {
.sidebar {
width: 500px; } }

@media 的 queries 允许互相嵌套使用,编译时,Sass 自动添加 and

1
2
3
4
5
6
7
@media screen {
.sidebar {
@media (orientation: landscape) {
width: 500px;
}
}
}

编译为

1
2
3
@media screen and (orientation: landscape) {
.sidebar {
width: 500px; } }

@media 甚至可以使用 SassScript(比如变量,函数,以及运算符)代替条件的名称或者值:

1
2
3
4
5
6
7
8
9
$media: screen;
$feature: -webkit-min-device-pixel-ratio;
$value: 1.5;

@media #{$media} and ($feature: $value) {
.sidebar {
width: 500px;
}
}

编译为

1
2
3
@media screen and (-webkit-min-device-pixel-ratio: 1.5) {
.sidebar {
width: 500px; } }

@if

@if 的表达式返回值不是 false 或者 null 时,条件成立,输出 {} 内的代码:

1
2
3
4
5
p {
@if 1 + 1 == 2 { border: 1px solid; }
@if 5 < 3 { border: 2px dotted; }
@if null { border: 3px double; }
}

编译为

1
2
p {
border: 1px solid; }

@for

@for 指令可以在限制的范围内重复输出格式,每次按要求(变量的值)对输出结果做出变动。这个指令包含两种格式:@for $var from <start> through <end>,或者 @for $var from <start> to <end>,区别在于 throughto 的含义:当使用 through 时,条件范围包含 的值,而使用 to 时条件范围只包含 的值不包含 的值。另外,$var 可以是任何变量,比如 $i<start><end> 必须是整数值。

1
2
3
@for $i from 1 through 3 {
.item-#{$i} { width: 2em * $i; }
}

编译为

1
2
3
4
5
6
.item-1 {
width: 2em; }
.item-2 {
width: 4em; }
.item-3 {
width: 6em; }

@each

@each 指令的格式是 $var in <list>, $var 可以是任何变量名,比如 $length 或者 $name,而 <list> 是一连串的值,也就是值列表。

@each 将变量 $var 作用于值列表中的每一个项目,然后输出结果,例如:

1
2
3
4
5
@each $animal in puma, sea-slug, egret, salamander {
.#{$animal}-icon {
background-image: url('/images/#{$animal}.png');
}
}

编译为

1
2
3
4
5
6
7
8
.puma-icon {
background-image: url('/images/puma.png'); }
.sea-slug-icon {
background-image: url('/images/sea-slug.png'); }
.egret-icon {
background-image: url('/images/egret.png'); }
.salamander-icon {
background-image: url('/images/salamander.png'); }

了解更多参考官方文档https://www.sass.hk/docs/


Sass语法
http://www.dwyblog.cn/2020/09/08/Sass语法/
作者
幸福来敲门
发布于
2020年9月8日
许可协议