Explain how to define a variable in SASS
Last Updated :
07 Jul, 2022
SASS is a short form of Syntactically Awesome Style Sheet which is an extension to Cascading StyleSheet (CSS). It is a preprocessor of CSS. It is compiled to CSS & is fully compatible with every version of CSS. Sass reduces the repetition of CSS and hence saves time. It allows using the variables, nested rules, mixins, functions, and more, all with a fully CSS-compatible syntax. It helps to organize a complex & large structured stylesheet in a well-organized manner & hence facilitates the reusability of code.SASS variables are non-changeable, which means if the variables are redeclared the previous value remains unchanged, unlike CSS. Thus SASS variables are also known as imperative.SASS variables can have only one value at a time, which means they can’t have different values of different elements. We can store the information related to numbers strings, lists, booleans, colors, and null in the SASS variables. It is an open-source free-to-use extension designed by Hampton Catlin and developed by Natalie Weizenbaum and Chris Eppstein in 2006.
Sass introduces several features that do not exist in CSS like variables that can be used to store data or information that you can use later. It is an important feature of Sass. In this article, we will learn how to define a variable in Sass & will understand its implementation through the example.
Sass Variables:
- Sass variables are declared once to store data that can be reused whenever required.
- If you modify the variable value once, the changes are reflected in all places whenever the variables are used.
- In Sass, we can store data in multiple types such as Numbers, Strings, booleans, lists, nulls, etc.
- Sass uses the dollar symbol ($) to declare a variable followed by the specified variable name and the value of that variable separated by a colon (:). The line must end with a semicolon (;). The variables can also be used with other values.
- You can also use underscores ( _ ) or Hyphens ( – ) to declare descriptive variable names.
- We can enhance the efficiency of Sass by performing multiple math operations on variables.
Syntax:
$var_Name : var-value;
Example: The below examples will demonstrate how to define variables in SASS.
Filename: style.scss
// Global variable declaration
$global__light: #f9f9f9;
$global__dark: #000;
$global__font: ("Poppins");
$global__f_size: 26;
$global__Max_width: 1280px;
div {
$local__green: #00f034;
// Global variable called
color: $global__dark;
border: 1px solid $local__green;
font: $global__font;
// Global variable called
max-width: $global__Max_width;
}
$global_sky: #0000ff;
p {
// Local variable declare
$local__margin: 10px 5px 20px 0;
color: $global__dark;
border: 1px solid $global_sky;
font-size: $global__f_size;
// Local variable called
margin: $local__margin;
}
Output: The generated CSS output will be:
Filename: style.css
div {
/* Global variable called */
color: #000;
border: 1px solid #00f034;
font-family: "Poppins";
/* Global variable called */
max-width: 1280px;
}
p {
color: #000;
border: 1px solid #0000ff;
font-size: 26;
/* Local variable called */
margin: 10px 5px 20px 0;
}
/*# sourceMappingURL=style.css.map */
Example 1: This example illustrates the SASS variables which are compiled to CSS variables & defining their scopes.
HTML
<!DOCTYPE html>
< html >
< head >
< title >Sass Variable</ title >
< style >
div {
/* Global variable called */
color: #000;
border: 1px solid #00f034;
font-family: "Poppins";
/* Global variable called */
max-width: 1280px;
}
p {
color: #000;
border: 1px solid #0000ff;
font-size: 26;
/* Local variable called */
margin: 10px 5px 20px 0;
}
</ style >
</ head >
< body >
< div >Welcome to GeeksforGeeks</ div >
< p >A Computer Science portal for geeks.</ p >
</ body >
</ html >
|
Output:

Scope of a Sass variable: Like other languages, Sass also has the concept of scope. In Sass, you can declare the scope of variable in two ways those are as follows:
- Global variables
- Local variables
Examples: Here, we can see how we can use the scope of Sass variables.
- Global Variable Scope: Global variables are declared at the top of the file and you can use them anywhere in the code. You can also use !global to declare a local variable as a global variable.
Filename: style.scss
// It is a global variable
$clr_primary: #a9a5f4;
div {
// Local variable with global scope
$clr_dark: #000 !global;
background-color: $clr_primary;
}
p {
background-color: $clr_dark;
}
And the generated CSS output will be:
Filename: style.css
div {
// Here, clr_primary variable assigned
background-color: #a9a5f4;
}
p {
// Here, clr_dark variable assigned
background-color: #000;
}
- Local Variable: Variables, which are declared in block or with parenthesis {} ie., inside the curly braces, are local variables and you can’t use them outside of the scope of that particular block.
If you try to use variable outside scope, this will generate an error saying “Compilation Error: Undefined variable”, as shown below example.
div {
$clr__dark: #000; // local variable
background-color: $clr_dark;
}
p {
// We cannot use this variable here.
// It will throw an error.
background-color: $clr_dark;
}
Style.scss:
div {
// Local variable scope
$clr_light: #f9f9f9;
background-color: $clr_light;
}
p {
// Local variable scope
$clr_dark: #000;
background-color: $clr_dark;
}
style.css: The generated CSS output will be:
div {
// Here, clr_light variable assigned
background-color: #f9f9f9;
}
p {
// Here, clr_dark variable assigned
background-color: #000;
}
Example 2: This example illustrates the use of the SASS variables by compiling them to CSS variables.
HTML
<!DOCTYPE html>
< html >
< head >
< title >Sass Variable</ title >
< style >
div {
/* Here, clr_light variable assigned */
background-color: #f9f9f9;
}
p {
/* Here, clr_dark variable assigned */
background-color: #000;
}
</ style >
</ head >
< body >
< div >Welcome to GeeksforGeeks</ div >
< p >A Computer Science portal for geeks.</ p >
</ body >
</ html >
|
Output:

Example 3: In this example, using $Color: red !global; instead of $Color: red; will make it global and accessible throughout the stylesheet. Thus, all the text will red-colored. The default scope of the variable will be overridden by !global. It may not be used to declare a new variable.
HTML
< html >
< head >
< title >SASS Variable Scope</ title >
< link rel = "stylesheet" href = "style.css" >
</ head >
< body >
< h1 >Welcome to GeeksforGeeks</ h1 >
< p >A computer science portal for geeks</ p >
</ body >
</ html >
|
style.scss:
$Color: green;
h1 {
$Color: red !global;
color: $Color;
}
p{
color: $Color;
}
style.css: The generated CSS output will be:
h1{
color : red;
}
p{
color : red;
}
Output:
Note: Like !global, we can also use !default to assign the desired value if the variable isn’t defined or its value is null.
Flow Control Scope: Using flow control scope, we can conditionally assign value to a variable. Variables don’t shadow the other variables at the same level, however, they are just assigned to those values.
Example 4: In this example, we will see how to apply conditions to the variable and if the condition becomes true, we will assign a certain value to it.
HTML
< html >
< head >
< title >SASS Flow Control Scope</ title >
< link rel = "stylesheet" href = "style.css" >
</ head >
< body >
< h1 >Welcome to GeeksforGeeks</ h1 >
</ body >
</ html >
|
SCSS Code :
$theme: true !default;
$color: green!default ;
@if $theme
{
$color: lighten($color,50%) ;
}
h1{
color: $color;
}
style.css: The generated CSS output will be:
h1 {
color: #80ff80; /*50% lighter than green*/
}
Explanation: In this example, $theme is set to True, by default. Thus, the if-condition holds true and the color will lighten to 50% of its original value. As seen in the output, the green color is lightened by 50%.
Output:
Difference between SASS variable & CSS variables:
S.No.
|
SASS Variable
|
CSS Variable
|
1. |
All the variables are compiled in Sass.
|
The variables are included in the CSS output & need not compile them.
|
2. |
The Sass variables contain only one value at a time.
|
The CSS variables can contain different values for different elements.
|
3. |
The Sass variables are imperative ie. if the values of the variables are changed that we have used then their previously used values will remain the same.
|
The CSS variables are declarative ie., if we change the value of the variable then it will affect both the values which are previously used values & later used values.
|
Reference: https://sass-lang.com/documentation
Similar Reads
How to define a variable in HTML5 ?
The <var> tag is used to define variables in HTML and specify mathematical equations or computer programs. The content inside the tag is usually displayed in italics in most browsers. Note: This tag is not deprecated but using CSS can create more dynamic effects. The <var> tag also suppo
1 min read
How to Declare a Variable in PL/SQL?
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database applications. Variables act as placeholders for data which enable us to manipulate and store information within our PL/SQL programs. Here, we will explore various methods of declaring variables in PL
5 min read
How to use a sass variable in nth child class ?
SASS is one of the most popular CSS extension languages which provides superpower to the normal CSS. The nth-child() class is a pseudo-class, when some HTML element has more than two elements of the same kind then we can use the nth-child class pseudo-class to provide styling to those elements witho
2 min read
How to define colors as variables in CSS ?
In CSS, we can define custom properties (often known as CSS variables), that offers us great flexibility to define a set of rules and avoid rewriting them again and again. We can also use custom properties to define colors. Example 1: <!DOCTYPE html> <html> <head> <title> How
2 min read
How to set a Variable to equal nothing in SASS?
A variable is like a container where we can store some data and then reuse it later in our code. Therefore, one can define it once and then reuse it everywhere. The value of the variable can be changed any time in the code and it would take effect everywhere when it is used next. There may be a situ
2 min read
What is Variable Interpolation in SASS ?
SASS is a strong CSS preprocessor that adds a number of features to improve the usability and maintainability of stylesheets. Variable interpolation is one such feature that permits the dynamic insertion of variable values into selectors and properties. In this article, we will learn the variable in
3 min read
How to represent a variable in Less ?
LESS stands for Leaner Style Sheets. It is a backward-compatible language extension for CSS. One of its features is that it lets you use variables in the style sheet. Variables can be used to store CSS values that may be reused. This makes it easier for the user by letting them define commonly used
2 min read
Less.js Variable Interpolation
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. This dynamic style sheet language gives CSS more functionality. LESS provides interoperability across browsers. A programming language called CSS pre-processor is
3 min read
How to define scope in the context of variables in LESS ?
Variable scope is used to specify the availability of variables in certain parts of the code. Firstly, the variables are searched from the local scope (i.e the variables which are declared within the code block of a particular selector); if they are not found, then they are searched from the parent
1 min read
SASS Variable in CSS calc() function
CSS is itself a good language for web designing but stylesheets are becoming more complex, larger, and even harder to maintain. Thatâs where a preprocessor like Sass comes in action and helps us to use features which donât even exist in CSS. The calc() function is a function that takes a single expr
2 min read