Table of contents
1.
Introduction
2.
SASS Variables
2.1.
Examples of Sass Variables
3.
Scope of a Variable
3.1.
Global Variables
3.2.
Scoped Variables
4.
Default Value of a Variable
5.
Shadowing of Variables
6.
Flow Control Rules of Variables
7.
Advanced Variable Functions
8.
Advantages of Sass Variables
9.
Frequently Asked Questions
10.
Key Takeaways
Last Updated: Mar 27, 2024

SASS Variables

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

Introduction

SASS stands for Syntactically Awesome Style Sheets. A preprocessor scripting language is interpreted or compiled into Cascading Style Sheets(CSS). SASS is an extension of CSS and is compatible with all CSS versions. It also helps reduce the repetition in CSS and save time.

Stylesheets get more extensive and complex as we start working on big projects, making it hard to handle the data, not to mention debugging it. Sass allows using features that do not exist in CSS, like variables, inheritance, built-in functions, nested rules, mixins, imports, etc. An endless number of frameworks are built using Sass like Compass, Bourbon, Susy, etc.

SASS Variables

Sass Variables allow defining a value once and re-using it in multiple places. These variables begin with a dollar sign($) and are set like CSS properties. With the help of Sass, we can store information in variables like

  • String
  • Number
  • Colors
  • Boolean
  • Nulls
  • Lists

 

Syntax:

$name: value;

 

Note: SASS Variables treats hyphens and underscore in the same way.

Examples of Sass Variables

Example 1: 

Sass File

.header {
  height: $control-height;
}
.sub-header {
  height: $control-height;
}

 

When translated into CSS, it will become:

.header {
  height: 40px;
}
.sub-header {
  height: 40px;
}

 

This feature of Sass is extremely useful in large projects where if a single value is used multiple times and required to be changed, we can change the value at one place that will take care of the variable’s value at all places in the project.

 

Example 2: The following example declares four variables named myFont, myColor, myFontSize, and myWidth that can be used anywhere in the project.

Output.html

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="input.css">
<body>
<h1>Hello World</h1>
<p>This is a paragraph.</p>
<div id="container">This is some text inside a container.</div>
</body>
</html>

 

Sass File

$myFont: Helvetica, sans-serif;
$myColor: red;
$myFontSize: 18px;
$myWidth: 680px;
body {
  font-family: $myFont;
  font-size: $myFontSize;
  color: $myColor;
}
#container {
  width: $myWidth;
}

 

When transpired into CSS, it will become: 

Input.css

body {
  font-family: Helvetica, sans-serif;
  font-size: 18px;
  color: red;
}
#container {
  width: 680px;
}

 

Output:

When the Sass file is transpired into the input.css file, it takes values assigned to the variable and generated output accordingly.

Scope of a Variable

Sass Variables can be declared anywhere in the document.

Global Variables

These variables are declared at the top of the file and can be used anywhere in the file.
 

Sass File

$global: purple;  
h1 {
    color: $global;
    border: 1px solid $global;
}

 

When Sass file is transpired into a.css, it will look like this: 

a.css

body {
    color: purple;
    border: 1px solid purple;
}

 

Output:

Scoped Variables

These variables are declared in a block/section of the document. These variables cannot be accessed outside the block in which they are declared.
 

Sass File

h1 {
    $local: plum;
    color: $local;
    border: 1px solid $local;
}

 

When Sass file is transpired into b.css, it will look like this: 

b.css

h1 {
    color: plum;
    border: 1px solid plum;
}

 

Output:

We cannot use $local outside the h1 block in the given example. 

Default Value of a Variable

Sass allows a user to declare some default values that can be used if the variable is not assigned any value. If a variable is assigned some value, that value is overwritten over the default value.

We define a default value in SASS using the !default tag.

Syntax:

$color: white !default;

 

Shadowing of Variables

SASS allows users to write two variables with the same name (one globally and one locally). In this situation, a user may change the value of the variable defined globally by changing the value of a local variable. To avoid such a situation, SASS provides the feature of shadowing in which writing a local variable inside the local scope doesn’t affect the value of the global variable.

Example:

$variable: global value;
.content {
  $variable: local value;
  value: $variable;
}
.sidebar {
  value: $variable;
}

 

Flow Control Rules of Variables

Flow Control Rules help define the variables' values based on the program's flow. This makes the code easier to understand, just like the other programming languages. It changes the values of some variables based on the value of some other variables or features.
 

SASS File

$bool: true;
$color: blue;
$bgcolor: green;
@if $bool
$color: violet;
$bgcolor: pink;
.nav
background-color: $bgcolor
color: $color

 

The above Sass file, when translated into CSS, will become

.nav {
	background-color: pink;
	color: violet;
}

 

Advanced Variable Functions

The SASS library also provides some advanced features for working with variables like:

  • meta. variable-exists (): It returns whether a variable with the given name exists or not in the current scope.
  • meta.global-variable-exists(): It returns whether a global variable with the given name exists or not in the current scope.

 

Advantages of Sass Variables

  • It helps reduce repetitions of the same thing in a single project.
  • It is possible to perform mathematical operations on the Sass variables.

 

Frequently Asked Questions

Q1: How do SASS Variables differ from CSS Variables?

Ans:

 

Q2: What do you understand by variable interpolation in SASS?

Ans: Interpolation is a new principle, and it is widely used in SASS. It refers to an insertion process. It allows users to interpolate SASS expressions into a basic SASS or CSS code. This means we can define selector name, quoted or unquoted strings, property name, CSS at-rules, etc., as a variable. 

We can interpolate an expression using #{ }.

Syntax:

......#{$variable-name}........

 

where …… represents some text.

 

Q3: Explain the use of the SASS @import function.

Ans: 

  • It facilitates extending the CSS import rule. We need to enable the import of SASS and SCSS files.
  • It can merge all the imported files into a single CSS file.
  • It is used to match and mix a file virtually.
  • A filename is required to import the function.
  • It provides document-style presentation, which is better than flat CSS.
  • It also helps in keeping a more organized responsive design project.

 

Key Takeaways

This blog discussed SASS variables along with some useful information and a few examples that will help in understanding the concept better.

SASS is a preprocessor scripting language interpreted or compiled into Cascading Style Sheets(CSS). SASS is an extension of CSS and is compatible with all CSS versions. It helps in reducing the repetition in CSS and saves time.

If you are a beginner and interested in exploring other fields such as web development, core computer subjects, or competitive programming. In that case, you can follow our guided path to get a good grip on your concepts.

Live masterclass