What if you could change your entire website's colors by editing just one line of code?
CSS custom properties vs SASS variables - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to change the main color of your website. You have to find and replace every color code manually in your CSS files.
This is slow and risky. You might miss some places or accidentally change the wrong color. It's hard to keep your styles consistent and update them quickly.
CSS custom properties and SASS variables let you store colors in one place. Change the value once, and it updates everywhere automatically.
body { color: #333333; } h1 { color: #333333; } button { background: #333333; }$main-color: #333333;
body { color: $main-color; } h1 { color: $main-color; } button { background: $main-color; }You can easily manage and update your website's look without hunting through many lines of code.
A designer changes the brand color. With variables, the developer updates one value, and the whole site matches the new brand instantly.
Manual color changes are slow and error-prone.
Variables store values in one place for easy updates.
CSS custom properties work in the browser; SASS variables work during build time.
Practice
Solution
Step 1: Identify CSS custom properties syntax
CSS custom properties always start with--and are live in the browser, meaning they can be changed dynamically.Step 2: Identify SASS variables syntax and behavior
SASS variables start with$and are replaced during the build process, so they don't exist in the browser.Final Answer:
CSS custom properties start with--and work in the browser at runtime. -> Option DQuick Check:
CSS variables =--prefix and runtime [OK]
- Confusing $ and -- prefixes
- Thinking SASS variables work in the browser
- Assuming CSS variables are replaced before runtime
Solution
Step 1: Recall SASS variable syntax
SASS variables start with$and use a colon:to assign values.Step 2: Check each option
$primary-color: #3498db;uses correct syntax.--primary-color: #3498db;uses the CSS custom property prefix.primary-color: #3498db;omits the$prefix.$primary-color == #3498db;uses double equals which is invalid.Final Answer:
$primary-color: #3498db; -> Option CQuick Check:
SASS variable = $name: value; [OK]
- Using -- instead of $ for SASS variables
- Using == instead of : for assignment
- Omitting $ prefix
:root { --main-color: red; }
.box { color: var(--main-color); }
$main-color: blue;
.box2 { color: $main-color; }Solution
Step 1: Understand CSS custom property usage
The--main-coloris set to red in:root. The class.boxusesvar(--main-color), so it will be red at runtime.Step 2: Understand SASS variable usage
The SASS variable$main-coloris blue and used in.box2. This is replaced during compilation, so.box2color will be blue in the final CSS.Final Answer:
.box text will be red; .box2 text will be blue -> Option BQuick Check:
CSS var = red; SASS var = blue [OK]
- Thinking SASS variables work at runtime
- Confusing which variable affects which class
- Assuming CSS variables are replaced before runtime
$color: green;
.text { color: var($color); }
Solution
Step 1: Understand var() function usage
Thevar()function is a CSS function used only for CSS custom properties (those starting with--).Step 2: Understand SASS variable usage
SASS variables start with$and are used directly withoutvar(). Usingvar($color)is invalid becausevar()expects a CSS custom property name.Final Answer:
The var() function is only for CSS custom properties, not SASS variables. -> Option AQuick Check:
var() = CSS vars only, not SASS [OK]
- Using var() with SASS variables
- Confusing $ and -- prefixes
- Thinking var() works for SASS variables
$primary-color: #0055ff;
:root { --primary-color: #0055ff; }
Solution
Step 1: Understand dynamic changes in the browser
CSS custom properties (starting with--) live in the browser and can be changed with JavaScript or media queries without recompiling CSS.Step 2: Understand SASS variable limitations
SASS variables are replaced during build time and cannot change after the CSS is loaded, so they are not suitable for dynamic theming.Final Answer:
Use CSS custom property--primary-colorbecause it can change dynamically in the browser. -> Option AQuick Check:
Dynamic theme = CSS vars [OK]
- Choosing SASS variables for runtime changes
- Thinking both together improve dynamic behavior
- Ignoring CSS custom properties' runtime flexibility
