Variables are used to store some information and can be reused.
Sass variables can store the following information:
String
Figures
Color value
Boolean value
List
Null value
Sass variable use $ symbol:
$variablename: value;
The following example sets four variables: myFont , myColor , myFontSize , and myWidth .
After the variable is declared, we can use it in the code: Convert the above code to CSS code, as follows: The scope of a Convert the above code to CSS code, as follows: Css Code: That’s for sure Now the Convert the above code to CSS code, as follows: Css code Note: all global variables are generally defined in the same file, such as: 13.3.1.
Sass code: ¶ $myFont: Helvetica, sans-serif;
$myColor: red;
$myFontSize: 18px;
$myWidth: 680px;
body {
font-family: $myFont;
font-size: $myFontSize;
color: $myColor;
}
#container {
width: $myWidth;
}
13.3.2. Css Code: ¶
body {
font-family: Helvetica, sans-serif;
font-size: 18px;
color: red;
}
#container {
width: 680px;
}
Sass scope ¶
Sass variable can only have an effect at the current level, as follows the h1 style of the green , p tag is for red . Sass code:$myColor: red;
h1 {
$myColor: green; // Only useful in h1, local scope
color: $myColor;
}
p {
color: $myColor;
}
h1 {
color: green;
}
p {
color: red;
}
13.3.3.
!global ¶ Sass , we can use the !global keyword to set the variable is global: Sass code$myColor: red;
h1 {
$myColor: green !global; // Global scope
color: $myColor;
}
p {
color: $myColor;
}
p style of the label becomes green .h1 {
color: green;
}
p {
color: green;
}
\_globals.scss and then we use the @include to include the file.