Challenge - 5 Problems
CSS Variable Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding CSS Variable Scope
Consider the following CSS code. What color will the text inside the
<div class='child'> be?CSS
:root {
--main-color: blue;
}
.parent {
--main-color: red;
color: var(--main-color);
}
.child {
color: var(--main-color);
}Attempts:
2 left
💡 Hint
Remember that CSS variables cascade and inherit from their closest ancestor that defines them.
✗ Incorrect
The
.child element inherits the --main-color variable from its closest ancestor with that variable defined, which is .parent. So the color is red.📝 Syntax
intermediate2:00remaining
CSS Variable Scope Syntax Error
Which option contains a syntax error that prevents the CSS variable from being applied correctly?
CSS
:root {
--main-bg-color: lightgray;
}
.container {
--main-bg-color: white
background-color: var(--main-bg-color);
}Attempts:
2 left
💡 Hint
Check punctuation carefully inside CSS blocks.
✗ Incorrect
CSS requires semicolons after each declaration. Missing semicolon after
--main-bg-color: white causes the next line to be invalid.❓ rendering
advanced2:00remaining
Effect of Variable Scope on Nested Elements
Given this CSS, what background color will the
<div class='nested'> have?CSS
:root {
--bg-color: yellow;
}
.outer {
--bg-color: green;
background-color: var(--bg-color);
}
.nested {
background-color: var(--bg-color);
}Attempts:
2 left
💡 Hint
CSS variables cascade down the DOM tree from the closest ancestor that defines them.
✗ Incorrect
If
.nested is inside .outer, it inherits --bg-color as green, so background is green.❓ selector
advanced2:00remaining
CSS Variable Scope with Multiple Selectors
Which selector correctly limits the scope of the CSS variable
--text-color to only .section elements?Attempts:
2 left
💡 Hint
Variables defined inside a selector only apply to elements matching that selector and their children.
✗ Incorrect
Option D defines
--text-color inside .section, so only .section and its children use red. Other elements use the :root value black.❓ accessibility
expert3:00remaining
Accessibility Impact of CSS Variable Scope on Color Contrast
You have a CSS variable
--text-color defined in :root as dark gray. Inside a .dark-mode container, it is redefined as light gray. Which statement about accessibility and variable scope is correct?CSS
:root {
--text-color: #333333;
}
.dark-mode {
--text-color: #cccccc;
}
p {
color: var(--text-color);
}Attempts:
2 left
💡 Hint
Think about how variable scope allows different colors in different parts of the page for better readability.
✗ Incorrect
Scoped CSS variables let you change colors inside containers like
.dark-mode to improve contrast and readability, which helps accessibility.