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
Recall & Review
beginner
What is a CSS variable?
A CSS variable is a custom property that stores a value you can reuse throughout your CSS. It starts with two dashes, like --main-color.
Click to reveal answer
beginner
How do you declare a CSS variable?
You declare a CSS variable inside a selector using the syntax: --variable-name: value;. For example, :root { --main-color: #3498db; } sets a variable named --main-color.
Click to reveal answer
beginner
How do you use a CSS variable in your styles?
Use the var() function with the variable name. For example, color: var(--main-color); applies the value stored in --main-color.
Click to reveal answer
beginner
Why use CSS variables instead of repeating values?
CSS variables make it easy to change a value in one place and have it update everywhere. This saves time and keeps your styles consistent.
Click to reveal answer
beginner
Where is the best place to declare global CSS variables?
Declare global CSS variables inside the :root selector. This makes them available everywhere in your CSS.
Click to reveal answer
How do you declare a CSS variable named --bg-color with the value #fff?
Abackground-color: --bg-color #fff;
B:root { --bg-color: #fff; }
Cvar(--bg-color) = #fff;
Dbody { bg-color = #fff; }
✗ Incorrect
CSS variables are declared inside selectors with two dashes and a colon, like --bg-color: #fff;.
How do you use a CSS variable named --text-color to set text color?
Acolor: var(--text-color);
Bcolor: --text-color;
Cvar(text-color);
Dtext-color: var(--text-color);
✗ Incorrect
Use the var() function with the variable name to apply its value.
What is the advantage of using CSS variables?
AThey are required for all CSS.
BThey make CSS run faster.
CThey replace HTML tags.
DThey let you reuse values and update them easily.
✗ Incorrect
CSS variables help keep your styles consistent and easy to update.
Where should you declare CSS variables to make them available everywhere?
A:root selector
BInside each HTML tag
COnly inside <style> tags
DInside JavaScript files
✗ Incorrect
Declaring variables in :root makes them global in your CSS.
Which of these is a correct CSS variable name?
Amain-font
B$main-font
C--main-font
Dvar-main-font
✗ Incorrect
CSS variable names must start with two dashes, like --main-font.
Explain how to create and use a CSS variable to set a background color.
Think about how you store a color once and reuse it.
You got /4 concepts.
Describe the benefits of using CSS variables in a website's style.
Imagine changing one color everywhere by changing just one place.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of CSS variables?
easy
A. To store reusable values like colors and sizes
B. To create new HTML elements
C. To write JavaScript code inside CSS
D. To add comments in CSS files
Solution
Step 1: Understand what CSS variables do
CSS variables hold values that can be reused throughout the stylesheet, such as colors or font sizes.
Step 2: Compare options with this purpose
Only To store reusable values like colors and sizes describes storing reusable values; others describe unrelated tasks.
Final Answer:
To store reusable values like colors and sizes -> Option A
Quick Check:
CSS variables = reusable values [OK]
Hint: CSS variables store values you reuse often [OK]
Common Mistakes:
Thinking CSS variables create HTML elements
Confusing CSS variables with JavaScript
Believing CSS variables add comments
2. Which is the correct way to define a CSS variable for a primary color globally?
easy
A. :root { --primary-color: #3498db; }
B. body { primary-color: #3498db; }
C. :root { primary-color = #3498db; }
D. html { --primary-color #3498db; }
Solution
Step 1: Recall CSS variable syntax
Variables are defined with two dashes and a colon inside a selector, usually :root for global scope.
B. Missing colon after --font-size in variable definition
C. Using var() incorrectly in h1 font-size
D. font-size property cannot use variables
Solution
Step 1: Check variable definition syntax
The variable definition is missing a colon after --font-size; it should be --font-size: 16px;
Step 2: Verify usage of var()
The usage var(--font-size) is correct in h1 font-size property.
Final Answer:
Missing colon after --font-size in variable definition -> Option B
Quick Check:
Variable definitions need colon after name [OK]
Hint: Variable definitions need colon after name [OK]
Common Mistakes:
Forgetting colon in variable definition
Thinking var() usage is wrong
Believing variable names can't start with --
5. You want to create a theme with two colors using CSS variables: primary as blue (#0000ff) and secondary as gray (#888888). How do you apply these variables to style a button's background and border color?