SASS Variables
Syntax
SASS variables are declared using the $
symbol, followed by the variable name and its value:
$primary-color: #333;
body {
color: $primary-color;
}
Example
Here is an example of using SASS variables to define and apply colors to different elements:
$primary-color: #333;
$secondary-color: #999;
body {
color: $primary-color;
background-color: $secondary-color;
}
h1 {
color: $secondary-color;
}
button {
background-color: $primary-color;
color: white;
}
Output
The above example would compile to the following CSS:
body {
color: #333;
background-color: #999;
}
h1 {
color: #999;
}
button {
background-color: #333;
color: white;
}
Explanation
SASS variables are a way to store values that can be reused throughout a stylesheet. They make it easy to update a value in one place and have it cascade throughout the entire stylesheet.
In the example above, we define the primary and secondary colors as variables, and then apply them to various elements throughout the stylesheet. This makes it easy to update the colors in one place and have them apply to all relevant elements.
Use
SASS variables are commonly used in large projects where consistency and maintainability are important. They are particularly useful for defining color schemes, font sizes, and other design-related values.
Important Points
- SASS variables are declared using the
$
symbol, followed by the variable name and its value - Variables can be reused throughout the stylesheet
- SASS variables make it easy to update values in one place and have them apply to all relevant elements
- SASS variables are commonly used in large projects for consistency and maintainability
Summary
SASS variables are a useful tool for defining and reusing values throughout a stylesheet. They make it easy to update values in one place and have them cascade throughout the entire stylesheet, which can save time and improve maintainability in large projects.