Yogesh Chauhan's Blog

What’s the difference between variables in CSS and SCSS (Sass)?

in CSS & SCSS on May 7, 2021

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:


--base-color: #ff4c4c;

This is how you can use it afterwards:


color: var(--base-color);

CSS variables have access to the DOM.

You can change the CSS variables based on CSS media queries or even using JavaScript.

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.



Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
add_filter function in WordPressWordPressHow to uninstall Cocoapods from the Mac OS?MiscellaneousHow to get previous days or next days in PHP?PHPIntroduction to Angular modules Part 3: NgModules vs JavaScript modules and Angular librariesAngular$this Keyword in PHPPHPRecursion in JavaScriptJavaScript