Bird
Raised Fist0
CSSmarkup~20 mins

Declaring variables in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
CSS Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the correct way to declare a CSS variable for primary color?
Choose the option that correctly declares a CSS variable named --primary-color with the value #3498db inside the :root selector.
A:root { var(--primary-color): #3498db; }
B:root { primary-color: #3498db; }
C:root { $primary-color: #3498db; }
D:root { --primary-color: #3498db; }
Attempts:
2 left
💡 Hint
CSS variables start with two dashes and are declared inside a selector like :root.
🧠 Conceptual
intermediate
2:00remaining
How do CSS variables inherit values?
If a CSS variable is declared inside a body selector, which statement is true about its availability?
AThe variable is available globally to the entire document.
BThe variable is only available to the <code>body</code> element itself.
CThe variable is available to all elements inside <code>body</code> unless overridden.
DThe variable is not available anywhere unless declared in <code>:root</code>.
Attempts:
2 left
💡 Hint
Think about how CSS inheritance works with selectors.
selector
advanced
2:00remaining
Which selector correctly uses a CSS variable for background color?
Given the variable --bg-color declared in :root, which CSS rule correctly applies it as a background color to all section elements?
Asection { background-color: --bg-color; }
Bsection { background-color: var(--bg-color); }
Csection { background-color: $bg-color; }
Dsection { background-color: var(bg-color); }
Attempts:
2 left
💡 Hint
CSS variables are accessed with the var() function and two dashes.
layout
advanced
2:00remaining
How can CSS variables help create a responsive layout?
Which option shows a CSS variable declaration that changes based on screen width using media queries?
A:root { --gap: 1rem; } @media (min-width: 600px) { :root { --gap: 2rem; } }
B:root { --gap: 1rem; } @media (min-width: 600px) { body { --gap: 2rem; } }
C:root { --gap: 1rem; } @media (min-width: 600px) { div { gap: var(--gap); } }
D:root { --gap: 1rem; } @media (min-width: 600px) { :root { gap: 2rem; } }
Attempts:
2 left
💡 Hint
Variables can be redefined inside media queries on the same selector.
accessibility
expert
2:00remaining
Why is it important to use CSS variables for color themes in accessible web design?
Which statement best explains the accessibility benefit of using CSS variables for color themes?
AThey allow easy switching between color themes to support users with different visual needs.
BThey reduce page load time by embedding colors directly in HTML.
CThey prevent users from changing colors, ensuring consistent branding.
DThey automatically adjust colors to meet contrast standards without extra code.
Attempts:
2 left
💡 Hint
Think about how changing variables can help users customize appearance.

Practice

(1/5)
1. What is the main purpose of declaring CSS variables inside :root?
easy
A. To limit variables only to the :root selector
B. To create variables that only work in inline styles
C. To make variables available globally throughout the CSS
D. To declare variables that only apply to JavaScript

Solution

  1. Step 1: Understand the role of :root in CSS

    The :root selector targets the highest-level element in the document, usually the <html> element.
  2. Step 2: Recognize variable scope

    Declaring variables inside :root makes them global, so they can be used anywhere in the CSS.
  3. Final Answer:

    To make variables available globally throughout the CSS -> Option C
  4. Quick Check:

    Global variables = :root declaration [OK]
Hint: Declare variables in :root for global access [OK]
Common Mistakes:
  • Thinking variables declared in :root are local
  • Confusing CSS variables with JavaScript variables
  • Assuming variables only work inline
2. Which of the following is the correct syntax to declare a CSS variable named --main-color with the value #3498db inside :root?
easy
A. :root { --main-color: #3498db; }
B. :root { main-color = #3498db; }
C. :root { $main-color: #3498db; }
D. :root { var(--main-color): #3498db; }

Solution

  1. Step 1: Recall CSS variable declaration syntax

    CSS variables start with two dashes -- and are declared with a colon : inside a selector block.
  2. Step 2: Check each option

    :root { --main-color: #3498db; } uses correct syntax: --main-color: #3498db;. Others use invalid symbols or keywords.
  3. Final Answer:

    :root { --main-color: #3498db; } -> Option A
  4. Quick Check:

    Correct syntax = --name: value; [OK]
Hint: Use --name: value; inside :root [OK]
Common Mistakes:
  • Using = instead of : to assign values
  • Missing the double dash -- prefix
  • Trying to use var() in declaration
3. Given the CSS below, what color will the paragraph text be?
:root {
  --text-color: #ff0000;
}
p {
  color: var(--text-color);
}
medium
A. Red
B. Black
C. Blue
D. Green

Solution

  1. Step 1: Identify the variable value

    The variable --text-color is set to #ff0000, which is red.
  2. Step 2: Check how the variable is used

    The paragraph uses color: var(--text-color);, so it will use the red color.
  3. Final Answer:

    Red -> Option A
  4. Quick Check:

    Variable value applied = red [OK]
Hint: Match variable value with var(--name) usage [OK]
Common Mistakes:
  • Confusing hex codes with color names
  • Forgetting to use var() to apply variables
  • Assuming default color if variable is declared
4. What is wrong with this CSS code?
:root {
  --bg-color #ffffff;
}
body {
  background-color: var(--bg-color);
}
medium
A. Background color property is misspelled
B. Using var() incorrectly to apply variable
C. Variable name missing double dashes '--' prefix
D. Missing colon ':' after variable name in declaration

Solution

  1. Step 1: Check variable declaration syntax

    The declaration --bg-color #ffffff; is missing a colon : between the variable name and value.
  2. Step 2: Verify usage of variable

    The usage background-color: var(--bg-color); is correct, so the error is only in declaration.
  3. Final Answer:

    Missing colon ':' after variable name in declaration -> Option D
  4. Quick Check:

    Declaration syntax requires ':' [OK]
Hint: Always put ':' between variable name and value [OK]
Common Mistakes:
  • Forgetting colon ':' in variable declaration
  • Using var() without parentheses
  • Misspelling property names
5. You want to create a theme with two colors: --primary-color and --secondary-color. You want --secondary-color to be 50% transparent version of --primary-color. Which CSS variable declaration correctly achieves this?
hard
A. :root { --primary-color: 0, 0, 255; --secondary-color: rgba(var(--primary-color), 0.5); }
B. :root { --primary-color: #0000ff; --secondary-color: #0000ff80; }
C. :root { --primary-color: #0000ff; --secondary-color: rgba(var(--primary-color), 0.5); }
D. :root { --primary-color: #0000ff; --secondary-color: var(--primary-color, 0.5); }

Solution

  1. Step 1: Understand CSS variable limitations

    CSS variables hold values as strings; you cannot directly use var(--primary-color) inside rgba() expecting it to split into RGB components.
  2. Step 2: Check each option

    :root { --primary-color: #0000ff; --secondary-color: rgba(var(--primary-color), 0.5); } tries to use rgba(var(--primary-color), 0.5) but --primary-color is a hex string, so this won't work.
    :root { --primary-color: 0, 0, 255; --secondary-color: rgba(var(--primary-color), 0.5); } declares --primary-color as RGB components but CSS variables cannot hold multiple values like that easily.
    :root { --primary-color: #0000ff; --secondary-color: #0000ff80; } uses a hex color with alpha channel #0000ff80 which is blue with 50% opacity, correctly representing a transparent version.
    :root { --primary-color: #0000ff; --secondary-color: var(--primary-color, 0.5); } misuses var() with a second parameter that is not opacity.
  3. Final Answer:

    :root { --primary-color: #0000ff; --secondary-color: #0000ff80; } -> Option B
  4. Quick Check:

    Use hex with alpha for transparency in variables [OK]
Hint: Use hex with alpha channel for transparent colors [OK]
Common Mistakes:
  • Trying to use var() inside rgba() with hex colors
  • Declaring RGB as comma-separated string in variable
  • Misusing var() fallback parameter as opacity