YogeshChauhan . com

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

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Social Media Colors: LESS VariablesMiscSQL Right JoinSQL/MySQLHow to check if the page is single post page in WordPress?WordPressHow to add a ribbon on top of a container using CSS?CSSThe difference between Class declarations and Class expressions in JavaScriptJavaScriptLearn how to use Self JOIN in SQL and MySQLSQL/MySQL