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 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
Composing and Extracting Components in ReactReactHow to set the permissions on all files and folders in Mac?MiscellaneousHow to add a select dropdown in React?ReactWhat’s the difference between a Framework and a Library?MiscellaneousCreate a responsive pricing table using simple HTML and CSSCSSGlobal JavaScript methods to convert variables to numbersJavaScript