Let’s understand variables in CSS first.
CSS Variables using the var() Function
In CSS, you can use two dashes (–) to define a variable and then var() function to use/insert the variable value.
CSS variables can have a local scope or a global scope.
This is how you can define variables in CSS:
This is how you can use it afterwards:
CSS variables have access to the DOM.
CSS variables, same like Sass (SCSS) variables, reduce repetition.
CSS Variable Example
Differences between Sass (SCSS) and CSS Variables
Sass (SCSS) Variables are compiled into a regular value just like we saw the compiled CSS in this define SCSS variables example.
CSS variables are actually included in the output. If you inspect element in the example above, you’ll see the CSS variable in the browser.
Sass (SCSS) Variables can have only one value at a time while CSS variables can have different values for different elements using local scope.
Sass (SCSS) Variables are imperative while CSS variables are declarative.
That means Sass variables can be changes over time and you don’t need to worry about the previous instance where it was used, because it will keep the old value for previous instance and new value for the new instance.
In CSS, once you change the value of the variable, it will be applied to all instances where it was used making it a declarative.