This article includes information about how to define a variable in SASS and scope of a variable.
Prerequisite:
Variables in CSS
Advantages of a variable:
- Reduces the repetition of the same thing again and again.
- We can perform mathematical operations over variable like +,-,/,* etc... , which makes our work lot more easier.
- Declaration of a variable in SASS: In SASS, you can define a variable by using $ symbol at the starting of the name of the variable and followed by its value.
- SCSS file:
css
$textcolor: blue;
$size: 10px;
$border_changes: 1px solid rgba(255,153,153,0.2);
#para1 {
color: $textcolor;
font-size: $size;
border: $border_changes;
}
// You can also use other variables
// in the declaration of a variable
$border_changes2: 1px solid $textcolor;
#para2 {
color: $textcolor;
font-size: $size;
border: $border_changes2;
}
- Compiled CSS file:
css
#para1 {
color: blue;
font-size: 10px;
border: 1px solid rgba(255, 153, 153, 0.2);
}
#para2 {
color: blue;
font-size: 10px;
border: 1px solid blue;
}
- Understanding scope of a variable: SASS variables can be declared anywhere in the document before it is used.
- Global Variables: Variables, which are declared at the top of the file, are global i.e., you can use it anywhere in the document.
- Scoped Variables: Variables, which are declared in any block, are scoped i.e., you can not use it outside of the scope of the block.
- SASS file:
css
$global: #FF0000;
#para1 {
$local: #F0F000;
color: $local;
border: 1px solid $local;
}
// You can not use $local outside of its
// #para1{ } block.
// This will generate error
// saying "undefined variable"
$global_onwards: #00FEF0;
h1 {
// You can not use $local here
color: $global;
// You can use $global_onwards,
border: 1px solid $global_onwards;
}
- Compiled CSS file:
There are more concepts on variables, like Shadowing of a variable, default value of a variable, Flow-control declaration of variables. Let's discuss them below one by one :
Default Value of Variables
SASS allows a user to declare some default values to variables which can be used if the variable is not declared or assigned a value of 'null'. If a variable is declared with some value , then that value is overwritten over the default value. In SASS , the default variable is assigned with the !default tag.
Syntax of default variable :
$color: black !default ; //black is the default value to the variable 'color'
Shadowing of Variables
In SASS , one can write two variables with exactly same name : one global and one local. In such condition , user may mistakenly change the value of global variable by changing the value of the local variable. To avoid such a condition, SASS has the shadowing feature, in which on writing a local variable inside a local scope doesn't necessarily change the value of the global variable.
Given below is an example to show the above functionality :
//SASS Code file
$colour: black;
.nav
$colour: blue;
background-color: $colour;
.p
background-color: $colour;
The above code when compiled to native CSS becomes:
CSS
.nav {
background-color: blue;
}
.p {
background-color: black;
}
Now, to change the value of the global variable from the local scope , we need to mention the "!global" tag beside the variable as shown below:
//SASS Code file\
$colour: black;
.nav
$colour: blue !global;
background-color: $colour;
.p
background-color: $colour;
The above code when compiled to native CSS becomes :
CSS
.nav {
background-color: blue;
}
.p {
background-color: blue;
}
Flow Control Rules of Variables
Flow Control Scope helps to define the values of the variables based on the flow of the program. They makes the code much easier to understand and makes the code just like the other programming languages like Java , C++ etc. It changes the values of some of the variables based on the value of some other variables or features. Given below is an example code that displays flow control rules of variables:
//SASS Code file
$bool: true;
$font_color: blue;
$bg_color: skyblue;
@if $bool
$font_color: violet;
$bg_color: pink;
.nav
background-color: $bg_color
color: $font_color
The above code when compiled to native CSS :
CSS
.nav {
background-color: pink;
color: violet;
}
Similar Reads
PHP Variables
A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.All variable names start with a dollar sign ($).Variables can store different data types, like integers, strin
5 min read
SASS | negative variable value
SASS variables are quite useful in writing maintainable styling code for web pages. Often, we require different components of a page to have the same value for a stylistic property. In such a scenario, assigning the value to a variable and using it wherever required reduces the amount of effort need
2 min read
CSS At-Rules
Sass supports every at-rule that is a part of the proper CSS. In order to stay flexible and future-compatible with the upcoming versions of CSS, Sass has given general support that covers almost every at-rules by itself. Syntax: css @<rule> <value>, @<rule> { ... } OR css @<rule
2 min read
SASS variable for images path
Whenever we want to add an image to our webpage, we need not always write the full path of our images when using SASS. We can rather store the path to the images folder in a variable. It is always good to add the path of a folder to a variable. Declaring a variable Syntax : $assetPath :"path"; Addin
1 min read
Bootstrap 5 Reboot Variables
Bootstrap5 Reboot Variables facilitates the <var> tag that is used to indicate the variables. Syntax: <var> Variable name </var>Example 1: This example describes the basic usage of the Bootstrap 5 Reboot Variables by implementing the <var> tag. HTML <!DOCTYPE html> <
1 min read
Variables and Datatypes in PHP
A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.$name = "GFG";$age = 30;Declaring Variables in PHPTo declare a variable, you simply use the dollar sign followed
4 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
SASS | Use variables across multiple files
To use variables across multiple files in SASS is carried out by @import rule of SASS. @import rule which import Sass and CSS stylesheets for providing variables, @mixins and functions.Such that combine all stylesheets together for compiled css. It also imports URL such as frameworks like Bootstrap
3 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
Bootstrap 5 Table SASS
Bootstrap 5 Table SASS can be used to change the default values provided for the table by customizing scss file.SASS variables of Table:$table-cell-padding-y: This variable provides the top and bottom padding of the table cell. By default, it is 0.5rem.$table-cell-padding-x: This variable provides t
7 min read