31

I don't understand the difference between CSS custom properties (variables) and SCSS variables (I am new to Sass by the way).

If this CSS code:

:root {
    --someColor: coral;
}
  
body {
    background: var(--someColor);
}

achieves the same results as this SCSS code:

$someColor: coral;
    
body {
    background: $someColor;
}

Why were SCSS variables introduced? Are they really the same as CSS variables, or am I missing something?

3
  • Refer : geeksforgeeks.org/what-is-the-difference-between-css-and-scss Commented Apr 9, 2021 at 7:02
  • 2
    @Sid I did refer to that, it does not answer my question which is specifically what is the difference between SCSS variables and CSS ones (and not the general difference between SCSS/CSS), thank you nevertheless! Commented Apr 9, 2021 at 7:06
  • It’s all in the browser which you could manipulate css variables And the browser will automatically change it. Commented Nov 16, 2021 at 13:04

1 Answer 1

44

SCSS is a preprocessor. That means it is not CSS, but is converted into CSS at 'compile time'. In the resulting CSS code there is no resemblance to the original SCSS code. Hence you cannot change the variable values at CSS 'runtime'.

Historically SCSS is a fairly old technique. Actually it dates back to as far as 2007. It was invented by the motivation that CSS lacks certain features amongst which are variables (and nesting and loops and mixins etc.).

CSS variables are a quite recent addition to the CSS standard (The last call working draft seams to be from 2014). They didn't render SCSS variables useless, because you can do things to SCSS variables which you can't do with CSS variables (to my knowledge) like color calculations.

On the other hand you can do things with CSS variables that you can't do with SCSS variables like changing at runtime.

BTW: CSS variables are not the official jargon. They should be called custom properties. But everyone calls them variables.

Addendum 1

One difference I was not talking about. You can't change SCSS variables with JavaScript. CSS Custom Properties however can be accessed and changed with JS

Addendum 2

This article on CSS Tricks has a good overview: https://css-tricks.com/difference-between-types-of-css-variables/

Addendum 3

As pointed out setting custom properties in CSS is the prefered way for reasons of freedom for later manipulation. However media queries can not handle custom propertien. So for media queries for instance things like breakpoints and the like SCCS or Sass variables are still (2024) the way to go.

Sign up to request clarification or add additional context in comments.

9 Comments

That explains why the compiled SCSS replaces the occurrences of the variable with its value instead of using the CSS custom properties! So you are saying it is due to historical reasons, because SCSS variables were introduced before CSS custom properties?
Historical reason is one point yes. The calculation is another
the term variable is indeed official but probably not used correctly. We talk about cascading variables and custom properties. These two features (defined in the same specification : w3.org/TR/css-variables-1) are reduced to be called CSS variables
@yamanidev no, they don't achieve the same purpose at all. That's the trap where everyone fall. CSS variables aren't in any way a replacement to SASS variables. They are two orthogonal feature. You may not notice the difference if you are new to CSS but when you will work with them for too long you will understand what I mean.
I will add SCSS variables will result in a compilation error if they aren't defined or can't be found. Useful for developing, IMO. Whereas css properties ( also called variables ) will silently fail when not found.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.