CSS custom properties and SASS variables both store values to reuse in styles. They help keep code clean and easy to change.
CSS custom properties vs SASS variables
Start learning this pattern below
Jump into concepts and practice - no test required
/* CSS custom property */ :root { --main-color: #3498db; } /* SASS variable */ $main-color: #3498db;
CSS custom properties start with two dashes and live inside selectors like :root.
SASS variables start with a dollar sign and are used in SASS files before compiling to CSS.
--padding is defined and used with var(--padding).:root {
--padding: 1rem;
}
.box {
padding: var(--padding);
}$padding is defined and used directly in styles.$padding: 1rem; .box { padding: $padding; }
:root {
--color: red;
}
.button {
color: var(--color);
}
/* Later in JS, you can change --color dynamically */$color: red; .button { color: $color; } /* SASS variables cannot change after compilation */
This example shows how CSS custom properties define colors in :root and are used in styles. The background and text colors come from the variables.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS Custom Properties vs SASS Variables</title> <style> :root { --main-bg-color: #f0f8ff; --main-text-color: #333; } body { background-color: var(--main-bg-color); color: var(--main-text-color); font-family: Arial, sans-serif; padding: 2rem; } .box { background-color: var(--main-text-color); color: var(--main-bg-color); padding: 1rem; border-radius: 0.5rem; max-width: 300px; margin-top: 1rem; } </style> </head> <body> <h1>Using CSS Custom Properties</h1> <div class="box">This box uses CSS custom properties for colors.</div> </body> </html>
CSS custom properties work in the browser and can be changed with JavaScript.
SASS variables exist only during development and are replaced with actual values when CSS is created.
Use CSS custom properties for dynamic theming and SASS variables for static values during build.
CSS custom properties are live in the browser and start with --.
SASS variables start with $ and are replaced before the browser sees the CSS.
Choose CSS variables for dynamic changes and SASS variables for fixed values during development.
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
