样式表的结构
就像 CSS 一样,大多数 Sass 样式表主要由包含属性声明的样式规则组成。但 Sass 样式表有更多可以与这些规则共存的特性。
语句语句 permalink
Sass 样式表由一系列_语句_组成,这些语句按顺序求值以构建最终的 CSS。某些语句可能有_块_,使用 {
和 }
定义,其中包含其他语句。例如,样式规则是一个带有块的语句。该块包含其他语句,如属性声明。
在 SCSS 中,语句用分号分隔(如果语句使用块,分号是可选的)。在缩进语法中,它们只是用换行符分隔。
通用语句通用语句 permalink
这些类型的语句可以在 Sass 样式表的任何位置使用:
- 变量声明,如
$var: value
。 - 流程控制 at-rules,如
@if
和@each
。 @error
、@warn
和@debug
规则。
CSS 语句CSS 语句 permalink
这些语句会生成 CSS。它们可以在除 @function
之外的任何地方使用:
- 样式规则,如
h1 { /* ... */ }
。 - CSS at-rules,如
@media
和@font-face
。 - 使用
@include
的混入使用。 @at-root
规则。
顶层语句顶层语句 permalink
这些语句只能在样式表的顶层使用,或者嵌套在顶层的 CSS 语句中:
其他语句其他语句 permalink
- 属性声明,如
width: 100px
,只能在样式规则和某些 CSS at-rules 中使用。 @extend
规则只能在样式规则中使用。
表达式表达式 permalink
表达式_是属性或变量声明右侧的任何内容。每个表达式都会生成一个值_。任何有效的 CSS 属性值也是 Sass 表达式,但 Sass 表达式比普通 CSS 值强大得多。它们作为参数传递给混入和函数,用于 @if
规则的控制流,并使用算术进行操作。我们称 Sass 的表达式语法为 SassScript。
字面量字面量 permalink
最简单的表达式只是表示静态值:
- 数字,可以有或没有单位,如
12
或100px
。 - 字符串,可以有或没有引号,如
"Helvetica Neue"
或bold
。 - 颜色,可以通过十六进制表示或名称引用,如
#c6538c
或blue
。 - 布尔字面量
true
或false
。 - 单例
null
。 - 值列表,可以用空格或逗号分隔,可以用方括号括起来,也可以不用括号,如
1.5em 1em 0 2em
、Helvetica, Arial, sans-serif
或[col1-start]
。 - 映射,将值与键关联,如
("background": red, "foreground": pink)
。
运算运算 permalink
Sass 定义了多种运算的语法:
==
and!=
are used to check if two values are the same.+
,-
,*
,/
, and%
have their usual mathematical meaning for numbers, with special behaviors for units that matches the use of units in scientific math.<
,<=
,>
, and>=
check whether two numbers are greater or less than one another.and
,or
, andnot
have the usual boolean behavior. Sass considers every value "true" except forfalse
andnull
.+
,-
, and/
can be used to concatenate strings.(
and)
can be used to explicitly control the precedence order of operations.