Table of contents
1.
Introduction
2.
Types of operators
3.
Assignment operators
4.
Arithmetic operators
5.
Equality Operators
6.
Comparison operators
7.
Logical operators
8.
String operations
9.
Order of operations
10.
FAQs
11.
Key Takeaways
Last Updated: Mar 27, 2024

Sass Operators

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Sass becomes more attractive to you as you learn and use its functions. Sass also gives you many exciting ways to use arithmetic operators and other operators.

The SASS language provides SASS operators that we can use to create a dynamic CSS rule, making CSS rule writing a pleasure. The order in which these operators are executed is standard.

Types of operators

Assignment operators

The colon represents the assignment operator. A value can be assigned to a variable using this operator.

new.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>assignment operator</h2>
    </body>
</html>

style.scss

h2 {
    color: pink;   
}

Output:

Arithmetic operators

As in standard arithmetic, these operators operate similarly. Despite this, we should note that arithmetic operations cannot be applied to values with a different unit.

Some Important facts about Arithmetic operators:

  • It is also possible to concatenate strings by using the addition ( + ) operator, but only for numbers in the same unit.
  • A number multiplied by two numbers of the same unit is invalid CSS.
  • Although the division operator is one of the CSS shorthand properties, Sass prefers to execute the division operation using a forward slash (/).
  • According to the BODMAS formula, SAS follows the operator precedence:

 

 Firstly, parenthetical expressions are evaluated followed by multiplication (*) and division (/), having a higher priority after which addition (+) and        subtraction (-)takes place.

 

List of Arithmetic Operators:

Operators

Description

+ It is used for addition.
- It is used for subtraction.
* It is used for multiplication.
/ It is used for division.
% It is used for getting remainder.

 

new.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>Addition</h2>

        <h3>Subtraction</h3>

        <h4>Multiplication</h4>

        <h5>Division</h5>
    </body>
</html>

style.scss

$size: 50px;

h2 {
   font-size: $size + 10;
}

h3 {
   font-size: $size - 5;
}

h4 {
   font-size: $size * 0.5;
}

h5 {
   font-size: $size / 2;
}

style.css

h2 {
  font-size: 60px;
}

h3 {
  font-size: 45px;
}

h4 {
  font-size: 25px;
}

h5 {
  font-size: 25px;
}

Output:

Equality Operators

If two values are equal, the equality operators return true else false. 

These are written as: <expression> == <expression>, which returns whether two expressions are equal, and <expression> !=<expression>, which returns whether two expressions are not equal.

List of Equality Operators:

Operators

Description

Operators

= = Checks if the values are equal = =
!= Checks if the values are not equal. !=

 

new.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>Equality operators</h2>
    </body>
</html>

style.scss

$size: 10px;  
h2 {  
    @if($size <= 20px) { 
        font-size: $size*2;  
    } @else {  
        font-size: $size / 2;  
    }  
} 

style.css

h2 {
  font-size: 20px;
}

Output:

Comparison operators

The conditional operator determines whether two numbers are greater or smaller. The operators automatically convert between compatible units.

List of Comparison operators:

Operators

Definition

> Specifies the value that is greater than the other.
>= Specifies the value that is greater than or equal to the other value.
< Specifies the value less than the other value.
<= Specifies the value that is less than or equal to the other value.

 

new.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>Comparison operators</h2>
    </body>
</html>

style.scss

$size: 10px;  
h2 {  
    @if($size <= 20px) {  
        font-size: $size*2;  
    } @else {  
        font-size: $size / 2;  
    }  
} 

style.css

h2 {
  font-size: 20px;
}

Output:

Logical operators

Sass uses words to represent its boolean operators instead of symbols in languages like JavaScript. Multiple conditions can be tested with logic operators within conditional expressions.

Consider two values, A and B, then:

Operators

Description

And A and B returns true if both expressions’ values are true, and false if either is false.

 

Or

 

A or B returns true if either expression’s value is true, and false if both are false.
Not A returns the opposite of the expression's value: it turns true into false and false into true.

 

new.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>logical operators</h2>
    </body>
</html>

style.scss

$val:30;
h2 {
   @if ($val > 20 and $val <= 30) {
      color: rgb(52, 50, 189);
   }
}

style.css

h2 {
  color: #3432bd;
}

Output:

String operations

Strings can be concatenated using the + operation.

new.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title></title>

        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <h2>String operator</h2>
    </body>
</html>

style.scss

h2 {
    content: "String " + operation;
    font-family: sans + "-serif";
}

style.css

h2 {
  content: "String operation";
  font-family: sans-serif;
}

Output:

Order of operations

SASS has a pretty standard order of operations, starting with the highest order of preference to the lowest order of preference are as follows:

S_no.

Symbols

Operators

1

( )

Parentheses

2

NOT,  +, -, /

Most of the unary operators

3

*   /   %

Multiplication, division, modulo operators

4

+   -

Addition and subtraction

5

>, >=, < and <=

Comparison operators

6

==   !=

Assignment operators

7

and

Logical operator

8

or

Logical operator

9

=

Equal to operator

FAQs

  1. Explain what the difference between Sass and SCSS is?
    The difference between Sass and SCSS is:
    Developed as an extension of CSS3 and having advanced syntax, Sass has two syntaxes: 
    → It uses the .scss extension, and the first syntax is "SCSS."
    → Another syntax is SASS or indented syntax, using the .sass extension.

    A valid CSS document can be converted to Sass by replacing the extension from.CSS to.SCSS. Sass has a loose syntax with white space and no semicolons. SCSS is more similar to CSS.
     
  2. What is a variable, and how is it defined in SASS?
    This information can be stored in SASS Variables for reusability throughout the style sheet. Start it with a dollar ($) sign to define a variable. These variables are needed to make the colors consistent across the application when using different colors.
     
  3. How many ways can we use SASS?
    Three ways can be used with SASS (Syntactically Awesome Style Sheets).
    → The SASS command-line tool can be used
    → Ruby modules can be used with SASS|
    → The Rack Enable Network can use SASS as a plugin

Key Takeaways

The compiler aids the implementation of operators by using symbols with special meanings. Sass supports the most common arithmetic, assignment, comparison (relational), and logical (conditional) operators. Sass does not support ternary or at-rule operators, neither of which are supported by Sass.

We change the precedence of operators using parentheses when certain operators are more important than others.

If you wish to learn web development, we recommend you to check out our guided path, which is absolutely free.

Live masterclass