0
0
CSSmarkup~20 mins

Declaring variables in CSS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.