Sass OperatorsSass facilitates you to do basic mathematical operations in the style sheet. It is very simple to apply the appropriate arithmetic symbol. Sass provides some standard math operators i.e. +, -, *, /, and %. Let's take an example to do some simple math to calculate width for an aside and article. SCSS Syntax: Equivalent Sass Syntax: This will create a very simple fluid grid, based on 960px. The Sass operation will do something like take pixel values and convert them to percentages without much hassle. It will generate CSS like this: Sass Operators List1) Assignment OperatorIn Sass, the colon ( : ) operator is used to define a variable. Syntax: 2) Arithmetic OperatorsThese are some standard math operators used to perform arithmetical operations. Sass supports the following arithmetic operators. A list of arithmetic operators:
Important Facts:
For example: For example: But Sass prefers forward slash (/) to perform the division operation. Let?s take an example to see how Sass understands this symbol. See this example: 3) Equality OperatorEquality operators are used in conditional statements. These operators are used to test if two values are same or not and return Boolean values. Sass supports the following equality operators.
These are supported for all types. Let's take two examples to understand it well. The equality operator ( ==) is used in first example to test the type of the $font argument and print the appropriate message. See this example: The generated CSS will have the following code: The second example defines a list and checks the length of its items. Here, remainder (%) operator is used to evaluate their length and then set the color property to the h2 element. See this example: The generated CSS will have the following code: Note: Sass doesn't support the strict equality operator (===) like other languages. 4) Comparison OperatorComparison operators are similar to equality operators but they are only used to compare numbers. A list of comparison operators supported in Sass:
See this example: After the compilation, the resulting CSS will have the following code. 5) Logical OperatorsLogical operators are used to test multiple conditions within a conditional expression. A list of logical operators supported in Sass:
Let's take an example to understand it well: This example contains button states as keys and the corresponding colors as values. Then, we specify a condition to evaluate its length. If the entire condition is true, it applies background-color to the h2 element. Sass Syntax: Next TopicSass Output Style |