Using variables in SCSS helps you store reusable values like colors, fonts, and spacing in one place, making your styles easier to manage. You can define a variable using the $ symbol, such as $primary-color: #3490dc;, and reuse it throughout your stylesheets. This ensures consistency and allows you to update values globally by changing just one line. Variables can be used for any CSS value, including sizes, margins, and font families. Overall, SCSS variables improve maintainability and make your code more efficient and scalable.
What Are SCSS Variables?
SCSS variables let you store reusable values like colors, fonts, and spacing. This helps maintain consistency and makes your code easier to update.
Example:
$primary-color: #3490dc;
body {
background-color: $primary-color;
}
Defining Variables
You can define variables using the $ symbol followed by a name. These can store any CSS value.
Example:
$font-size: 16px;
$padding: 10px;
p {
font-size: $font-size;
padding: $padding;
}
Using Variables in Styles
Once defined, variables can be reused anywhere in your SCSS file. This avoids repetition and improves maintainability.
Example:
$main-color: #ff5733;
h1 {
color: $main-color;
}
button {
background-color: $main-color;
}
Updating Values Easily
One of the biggest advantages of variables is that you can update a value in one place and it reflects everywhere.
Example:
$theme-color: #28a745; // Change this once
.header {
background: $theme-color;
}
.footer {
background: $theme-color;
}
Using Variables with Calculations
SCSS allows you to perform operations with variables, like addition, subtraction, and multiplication.
Example:
$base-padding: 10px;
.container {
padding: $base-padding * 2; // 20px
}



