0
0
CSSmarkup~20 mins

Variable scope in CSS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS Variable Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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);
}
AThe text will be blue because :root variables override local ones.
BThe text will be black because the child does not define its own variable.
CThe text will be red because the child inherits the parent's variable value.
DThe text will be transparent because the variable is undefined in the child.
Attempts:
2 left
💡 Hint
Remember that CSS variables cascade and inherit from their closest ancestor that defines them.
📝 Syntax
intermediate
2: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);
}
AVariables cannot be defined inside classes, only in :root.
BMissing semicolon after <code>--main-bg-color: white</code> in <code>.container</code> block.
CUsing <code>var(--main-bg-color)</code> instead of <code>var(main-bg-color)</code> causes error.
DBackground color property should be <code>background</code> not <code>background-color</code>.
Attempts:
2 left
💡 Hint
Check punctuation carefully inside CSS blocks.
rendering
advanced
2: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);
}
AGreen, because .nested inherits from .outer if nested inside it.
BYellow, because :root variable applies globally.
CTransparent, because .nested does not define the variable itself.
DBlue, because default background color is blue.
Attempts:
2 left
💡 Hint
CSS variables cascade down the DOM tree from the closest ancestor that defines them.
selector
advanced
2:00remaining
CSS Variable Scope with Multiple Selectors
Which selector correctly limits the scope of the CSS variable --text-color to only .section elements?
A
:root {
  --text-color: red;
}
.section {
  color: var(--text-color);
}
B
.section {
  --text-color: red;
}
.article {
  color: var(--text-color);
}
C
.section {
  --text-color: red;
  color: var(--text-color);
}
.article {
  color: var(--text-color);
}
D
:root {
  --text-color: black;
}
.section {
  --text-color: red;
  color: var(--text-color);
}
Attempts:
2 left
💡 Hint
Variables defined inside a selector only apply to elements matching that selector and their children.
accessibility
expert
3: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);
}
AText inside <code>.dark-mode</code> will have better contrast on dark backgrounds due to scoped variable override.
BScoped variables do not affect accessibility because colors are fixed by browser defaults.
CRedefining variables inside containers breaks keyboard navigation accessibility.
DVariables defined in :root override all scoped variables, so no contrast change occurs.
Attempts:
2 left
💡 Hint
Think about how variable scope allows different colors in different parts of the page for better readability.