CSS variables are declared with two dashes before the name and a colon to assign the value inside a selector like :root.
Step 2: Check each option for syntax correctness
:root { --main-color: #3498db; } uses :root { --main-color: #3498db; } which is correct. Others use wrong selectors or assignment operators.
Final Answer:
:root { --main-color: #3498db; } -> Option B
Quick Check:
CSS variable syntax = colon and double dash [OK]
Hint: CSS variables start with -- and use colon : [OK]
Common Mistakes:
Using = instead of : to assign values
Omitting the double dashes -- before variable name
Declaring variables outside a selector block
3. What will be the background color of the <div> in this CSS?
:root { --bg-color: lightgreen; } div { background-color: var(--bg-color); }
medium
A. lightgreen
B. white
C. transparent
D. black
Solution
Step 1: Understand variable declaration and usage
The variable --bg-color is set to lightgreen in :root, making it global.
Step 2: Check how variable is used in div
The div uses background-color: var(--bg-color); which applies the variable's value.
Final Answer:
lightgreen -> Option A
Quick Check:
var(--bg-color) = lightgreen [OK]
Hint: var(--name) uses the variable's value [OK]
Common Mistakes:
Forgetting to use var() to access variables
Assuming default color if variable is set
Confusing variable name syntax
4. Identify the error in this CSS code using variables:
:root { --text-color: blue } p { color: var(text-color); }
medium
A. Missing semicolon after variable declaration
B. Variables cannot be used inside p selector
C. Incorrect variable name usage inside var() function
D. Variable names cannot have dashes
Solution
Step 1: Check variable declaration syntax
The variable --text-color is declared correctly except missing semicolon, but CSS allows last property without semicolon.
Step 2: Check variable usage syntax
Inside var(), the variable name must include the double dashes --. Here it is missing.
Final Answer:
Incorrect variable name usage inside var() function -> Option C
Quick Check:
var() needs -- before variable name [OK]
Hint: Use var(--variable-name), include the dashes [OK]
Common Mistakes:
Omitting -- inside var()
Thinking semicolon is always required last
Believing variables can't be used in selectors
5. You want to create a website theme with a main color that can be changed easily. Which approach using CSS variables is best?
hard
A. Declare --main-color in :root and use var(--main-color) everywhere
B. Write the color value directly in every CSS rule
C. Use JavaScript to change colors without CSS variables
D. Create separate CSS files for each color and switch files
Solution
Step 1: Understand the goal of easy theme color changes
Using CSS variables declared in :root allows changing one value to update all uses.
Step 2: Evaluate each option for maintainability
Declare --main-color in :root and use var(--main-color) everywhere uses variables globally, making updates simple. Options B, C, and D are less efficient or more complex.
Final Answer:
Declare --main-color in :root and use var(--main-color) everywhere -> Option A
Quick Check:
Global CSS variables simplify theme changes [OK]
Hint: Use :root variables for easy global changes [OK]