Introduction

SCSS (Sassy CSS) is a popular extension of CSS that offers powerful features like variables, nesting, mixins and more. Among these features, SCSS variables play a crucial role in simplifying the process of managing and maintaining stylesheets. In this article, we will explore SCSS variables and understand how they can be used.

What are SCSS Variables?

Variables in SCSS are placeholders that store values, such as colors, fonts, sizes or any other CSS property. By assigning values to variables, you can reuse them throughout your stylesheet.

Declaring Variables

In SCSS, variables are declared using the $ symbol followed by the variable name and the value assignment. Let’s look at an example:

$primary-color: #ff0000;
$font-size: 16px;

In the example above, we declared two variables: $primary-color and $font-size. The first variable holds a hexadecimal color value, while the second one holds a font size value in pixels.

Using Variables

Once variables are declared, you can use them anywhere within your SCSS stylesheets. To use a variable, simply reference its name preceded by the $ symbol. Here’s an example:

.button {
  background-color: $primary-color;
  font-size: $font-size;
}

In the example above, the .button class uses the $primary-color variable for its background color and the $font-size variable for its font size. If you decide to change the primary color or font size, you only need to update the variable declaration, and the changes will be automatically applied to all elements using those variables.

Variable Scope

SCSS variables have scope, which means they can be accessed only within the scope in which they are defined. Variables declared outside any selector or rule have a global scope and can be accessed anywhere in the stylesheet. However, variables declared within a selector or rule have a local scope and are only accessible within that scope.

$global-color: #ff0000;

.header {
  $local-color: #0000ff;
  color: $local-color; // accessible within the .header scope
}

.button {
  color: $global-color; // accessible anywhere within the stylesheet
  background-color: $local-color; // throws an error as $local-color is not accessible here
}

In the example above, $global-color is accessible within both the .header and .button selectors. However, $local-color is only accessible within the .header selector. Trying to access $local-color within the .button selector would result in an error.

Modifying Variables

Once a variable is assigned a value, you can modify it later in your stylesheet. This allows you to update styles globally by modifying the variable’s value. Here’s an example:

$primary-color: #ff0000;

.button {
  background-color: $primary-color;
}

// Later in the stylesheet...

$primary-color: #00ff00;

// The button background color will now be #00ff00

By reassigning the value of $primary-color, all elements using that variable will be automatically updated with the new value.

Conclusion

SCSS variables provide a powerful way to manage and reuse styles in your CSS. By using variables, you can easily make global style changes and organize your stylesheets effectively. In this article we have covered SCSS varaibles.

Thank you for reading