字符串运算符

Sass 支持一些生成 字符串 的运算符:

  • <expression> + <expression> 返回包含两个表达式值的字符串。如果任一值为 带引号的字符串,则结果将被引用;否则,它将不被引用。

    ¥<expression> + <expression> returns a string that contains both expressions’ values. If the either value is a quoted string, the result will be quoted; otherwise, it will be unquoted.

  • <expression> - <expression> 返回一个不带引号的字符串,其中包含两个表达式的值,并用 - 分隔。这是一个旧版运算符,通常应使用 插值法 来代替。

    ¥<expression> - <expression> returns an unquoted string that contains both expressions’ values, separated by -. This is a legacy operator, and interpolation should generally be used instead.

Playground

SCSS Syntax

@debug "Helvetica" + " Neue"; // "Helvetica Neue"
@debug sans- + serif; // sans-serif
@debug sans - serif; // sans-serif
Playground

Sass Syntax

@debug "Helvetica" + " Neue"  // "Helvetica Neue"
@debug sans- + serif  // sans-serif
@debug sans - serif  // sans-serif

这些运算符不仅仅适用于字符串!它们可以与任何可写入 CSS 的值一起使用,但有一些例外:

¥These operators don’t just work for strings! They can be used with any values that can be written to CSS, with a few exceptions:

Playground

SCSS Syntax

@debug "Elapsed time: " + 10s; // "Elapsed time: 10s";
@debug true + " is a boolean value"; // "true is a boolean value";
Playground

Sass Syntax

@debug "Elapsed time: " + 10s  // "Elapsed time: 10s";
@debug true + " is a boolean value"  // "true is a boolean value";

⚠️ Heads up!

使用 插值法 创建字符串通常比依赖这些运算符更干净、更清晰。

¥It’s often cleaner and clearer to use interpolation to create strings, rather than relying on these operators.

一元运算符一元运算符 permalink

¥Unary Operators

由于历史原因,Sass 还支持 /- 作为一元运算符,它们只接受一个值:

¥For historical reasons, Sass also supports / and - as a unary operators which take only one value:

  • /<expression> 返回一个以 / 开头、后跟表达式值的不带引号的字符串。

    ¥/<expression> returns an unquoted string starting with / and followed by the expression’s value.

  • -<expression> 返回一个以 - 开头、后跟表达式值的不带引号的字符串。

    ¥-<expression> returns an unquoted string starting with - and followed by the expression’s value.

Playground

SCSS Syntax

@debug / 15px; // /15px
@debug - moz; // -moz
Playground

Sass Syntax

@debug / 15px  // /15px
@debug - moz  // -moz