sass-site/source/documentation/operators/string.liquid

78 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-06-06 20:57:55 +02:00
---
title: String Operators
introduction: >
2023-06-09 03:44:39 +02:00
Sass supports a few operators that generate
[strings](/documentation/values/strings):
2023-06-06 20:57:55 +02:00
---
2023-06-09 03:44:39 +02:00
2023-06-06 20:57:55 +02:00
{% markdown %}
2023-06-09 03:44:39 +02:00
* `<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.
2023-06-06 20:57:55 +02:00
2023-06-09 03:44:39 +02:00
* `<expression> - <expression>` returns an unquoted string that contains both
2023-06-06 20:57:55 +02:00
expressions' values, separated by `-`. This is a legacy operator, and
[interpolation][] should generally be used instead.
[quoted string]: /documentation/values/strings#quoted
[interpolation]: /documentation/interpolation
{% endmarkdown %}
{% codeExample 'string', false %}
@debug "Helvetica" + " Neue"; // "Helvetica Neue"
@debug sans- + serif; // sans-serif
@debug sans - serif; // sans-serif
===
@debug "Helvetica" + " Neue" // "Helvetica Neue"
@debug sans- + serif // sans-serif
@debug sans - serif // sans-serif
{% endcodeExample %}
{% markdown %}
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:
2023-06-09 03:44:39 +02:00
* Numbers can't be used as the left-hand value, because they have [their own
2023-06-06 20:57:55 +02:00
operators][numeric].
2023-06-09 03:44:39 +02:00
* Colors can't be used as the left-hand value, because they used to have
[their own operators][color].
2023-06-06 20:57:55 +02:00
[numeric]: /documentation/operators/numeric
[color]: /documentation/operators
{% endmarkdown %}
{% codeExample 'string-exceptions', false %}
@debug "Elapsed time: " + 10s; // "Elapsed time: 10s";
@debug true + " is a boolean value"; // "true is a boolean value";
===
@debug "Elapsed time: " + 10s // "Elapsed time: 10s";
@debug true + " is a boolean value" // "true is a boolean value";
{% endcodeExample %}
{% headsUp %}
It's often cleaner and clearer to use [interpolation][] to create strings,
rather than relying on these operators.
[interpolation]: /documentation/interpolation
{% endheadsUp %}
{% markdown %}
## Unary Operators
For historical reasons, Sass also supports `/` and `-` as a unary operators
which take only one value:
2023-06-09 03:44:39 +02:00
* `/<expression>` returns an unquoted string starting with `/` and followed by
2023-06-06 20:57:55 +02:00
the expression's value.
2023-06-09 03:44:39 +02:00
* `-<expression>` returns an unquoted string starting with `-` and followed by
2023-06-06 20:57:55 +02:00
the expression's value.
{% endmarkdown %}
2023-06-08 23:10:09 +02:00
{% codeExample 'unary-operators', false %}
2023-06-06 20:57:55 +02:00
@debug / 15px; // /15px
@debug - moz; // -moz
===
@debug / 15px // /15px
@debug - moz // -moz
{% endcodeExample %}