样式表的结构

就像 CSS 一样,大多数 Sass 样式表主要由包含属性声明的样式规则组成。但 Sass 样式表有更多可以与这些规则共存的特性。

语句语句 permalink

Sass 样式表由一系列_语句_组成,这些语句按顺序求值以构建最终的 CSS。某些语句可能有_块_,使用 {} 定义,其中包含其他语句。例如,样式规则是一个带有块的语句。该块包含其他语句,如属性声明。

SCSS 中,语句用分号分隔(如果语句使用块,分号是可选的)。在缩进语法中,它们只是用换行符分隔。

通用语句通用语句 permalink

这些类型的语句可以在 Sass 样式表的任何位置使用:

CSS 语句CSS 语句 permalink

这些语句会生成 CSS。它们可以在除 @function 之外的任何地方使用:

顶层语句顶层语句 permalink

这些语句只能在样式表的顶层使用,或者嵌套在顶层的 CSS 语句中:

其他语句其他语句 permalink

  • 属性声明,如 width: 100px,只能在样式规则和某些 CSS at-rules 中使用。
  • @extend 规则只能在样式规则中使用。

表达式表达式 permalink

表达式_是属性或变量声明右侧的任何内容。每个表达式都会生成一个_。任何有效的 CSS 属性值也是 Sass 表达式,但 Sass 表达式比普通 CSS 值强大得多。它们作为参数传递给混入函数,用于 @if 规则的控制流,并使用算术进行操作。我们称 Sass 的表达式语法为 SassScript

字面量字面量 permalink

最简单的表达式只是表示静态值:

  • 数字,可以有或没有单位,如 12100px
  • 字符串,可以有或没有引号,如 "Helvetica Neue"bold
  • 颜色,可以通过十六进制表示或名称引用,如 #c6538cblue
  • 布尔字面量 truefalse
  • 单例 null
  • 值列表,可以用空格或逗号分隔,可以用方括号括起来,也可以不用括号,如 1.5em 1em 0 2emHelvetica, 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, and not have the usual boolean behavior. Sass considers every value "true" except for false and null.
  • +, -, and / can be used to concatenate strings.
  • ( and ) can be used to explicitly control the precedence order of operations.

其他表达式其他表达式 permalink

  • 变量,如 $var
  • 函数调用,如 nth($list, 1)var(--main-bg-color),可以调用 Sass 核心库函数或用户定义的函数,或直接编译为 CSS
  • 特殊函数,如 calc(1px + 100%)url(http://myapp.com/assets/logo.png),它们有自己独特的解析规则。
  • 父选择器&
  • !important,它被解析为不带引号的字符串。