Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using CSS Custom Properties and SASS Variables
📖 Scenario: You are creating a simple webpage style where colors and font sizes are controlled by variables. You want to see how CSS custom properties and SASS variables work differently.
🎯 Goal: Build a stylesheet that uses both CSS custom properties and SASS variables to style a heading and a paragraph. You will define colors and font sizes using both methods and apply them to the text.
📋 What You'll Learn
Create CSS custom properties for --main-color and --main-font-size with exact values
Create SASS variables $secondary-color and $secondary-font-size with exact values
Use the CSS custom properties to style the h1 color and font size
Use the SASS variables to style the p color and font size
💡 Why This Matters
🌍 Real World
Web developers often use CSS custom properties and SASS variables to manage colors and fonts consistently across a website.
💼 Career
Understanding the difference between CSS custom properties and SASS variables helps developers write flexible and maintainable stylesheets.
Progress0 / 4 steps
1
Define CSS custom properties
Create a :root selector and inside it define CSS custom properties --main-color with value #3498db and --main-font-size with value 2rem.
SASS
Hint
Use the :root selector to define CSS variables with -- prefix.
2
Create SASS variables
Create two SASS variables: $secondary-color with value #e74c3c and $secondary-font-size with value 1.2rem.
SASS
Hint
Use $ to start SASS variable names and assign values with a colon.
3
Use CSS custom properties in h1
Write a CSS rule for h1 that sets color to the CSS custom property --main-color and font-size to --main-font-size using the var() function.
SASS
Hint
Use var(--variable-name) to access CSS custom properties inside CSS rules.
4
Use SASS variables in p
Write a CSS rule for p that sets color to the SASS variable $secondary-color and font-size to $secondary-font-size.
SASS
Hint
Use SASS variables directly by their names with $ inside CSS rules.
Practice
(1/5)
1. Which of the following is true about CSS custom properties compared to SASS variables?
easy
A. SASS variables start with -- and are dynamic in the browser.
B. SASS variables start with -- and work in the browser at runtime.
C. CSS custom properties start with $ and are replaced before the browser sees CSS.
D. CSS custom properties start with -- and work in the browser at runtime.
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 D
Quick Check:
CSS variables = -- prefix and runtime [OK]
Hint: CSS vars use -- and work live; SASS vars use $ and compile away [OK]
Common Mistakes:
Confusing $ and -- prefixes
Thinking SASS variables work in the browser
Assuming CSS variables are replaced before runtime
2. Which is the correct way to declare a SASS variable for a color?
easy
A. primary-color: #3498db;
B. --primary-color: #3498db;
C. $primary-color: #3498db;
D. $primary-color == #3498db;
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 C
Quick Check:
SASS variable = $name: value; [OK]
Hint: SASS vars start with $ and use colon for assignment [OK]
Common Mistakes:
Using -- instead of $ for SASS variables
Using == instead of : for assignment
Omitting $ prefix
3. Given the following code, what color will the text be in the browser?