Variable scope in CSS decides where a variable can be used. It helps keep styles organized and avoid mistakes.
Variable scope in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
:root { --main-color: blue; }
section { --section-color: green; }Variables defined in :root are global and can be used anywhere.
Variables defined inside a selector like section are local to that selector and its children.
:root {
--main-bg: lightgray;
}
body {
background-color: var(--main-bg);
}--text-color variable is local to article. Paragraphs outside article use black as fallback.article {
--text-color: darkred;
color: var(--text-color);
}
p {
color: var(--text-color, black);
}:root {
--font-size: 1rem;
}
header {
--font-size: 1.5rem;
font-size: var(--font-size);
}
footer {
font-size: var(--font-size);
}The global variable --main-color is blue. Inside the section, it is overridden to green. Paragraphs inside the section show green text, others show blue.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS Variable Scope Example</title> <style> :root { --main-color: blue; } section { --main-color: green; color: var(--main-color); padding: 1rem; border: 2px solid var(--main-color); margin-bottom: 1rem; } p { color: var(--main-color); } </style> </head> <body> <p>This paragraph uses the global main color.</p> <section> <p>This paragraph uses the section's main color.</p> </section> <p>This paragraph again uses the global main color.</p> </body> </html>
Use :root to define variables that should be available everywhere.
Variables inside selectors only affect that selector and its children.
If a variable is not found locally, CSS looks up to the parent scopes until it finds it or uses fallback.
CSS variables have scope: global (usually :root) or local (inside selectors).
Local variables override global ones inside their scope.
Understanding scope helps keep styles organized and predictable.
Practice
Solution
Step 1: Understand global scope in CSS variables
CSS variables defined inside:rootare global and accessible anywhere.Step 2: Compare with local scopes
Variables inside class or ID selectors are local and not accessible globally.Final Answer:
Inside the:rootselector -> Option BQuick Check:
Global CSS variables =:root[OK]
:root [OK]- Thinking variables in classes are global
- Confusing media queries with variable scope
- Assuming ID selectors define global variables
--main-color with value blue inside a class .header?Solution
Step 1: Recall CSS variable declaration syntax
CSS variables start with two dashes and use colon to assign value, e.g.,--var-name: value;.Step 2: Check each option
.header { --main-color: blue; } uses correct syntax. .header { var(--main-color): blue; } incorrectly usesvar()on left side. .header { --main-color = blue; } uses equals sign which is invalid. .header { main-color: blue; } misses dashes.Final Answer:
.header { --main-color: blue; } -> Option DQuick Check:
Variable declaration =--name: value;[OK]
--name: value; inside selectors [OK]- Using equals sign instead of colon
- Using var() on left side of declaration
- Omitting the double dashes
<div class='box'> be?:root { --text-color: black; } .box { --text-color: red; color: var(--text-color); }Solution
Step 1: Identify variable scopes
--text-coloris globally black in:root, but locally red inside.box.Step 2: Determine which variable applies
Inside.box, local variable overrides global, socolor: var(--text-color)uses red.Final Answer:
Red -> Option AQuick Check:
Local variable overrides global = red [OK]
- Assuming global variable always applies
- Confusing variable name with property name
- Ignoring local variable overrides
--bg-color is not applying inside .card?:root { --bg-color: yellow; } .card { background-color: var(--bg-color); } .card .content { --bg-color: green; }Solution
Step 1: Understand variable scope in nested selectors
Variables defined inside.card .contentare local to that selector only.Step 2: Check where
background-coloris appliedbackground-coloris set on.card, which uses the global variable from:root.Final Answer:
The variable--bg-colorinside.contentdoes not affect.card-> Option AQuick Check:
Local variables only affect their own scope [OK]
- Thinking nested variables override parents
- Believing variables can't be used in properties
- Misunderstanding variable naming rules
--button-color to be blue globally but red only inside .alert buttons. Which CSS setup achieves this correctly?Solution
Step 1: Define global variable in
Setting:root--button-color: bluein:rootmakes it global.Step 2: Override variable locally inside
Setting.alert button--button-color: redinside.alert buttonoverrides global only there.Step 3: Use variable in
Usingbuttoncolor propertycolor: var(--button-color)inbuttonapplies correct color depending on scope.Final Answer:
:root { --button-color: blue; } .alert button { --button-color: red; } button { color: var(--button-color); } -> Option CQuick Check:
Global in:root, local override in selector [OK]
:root, override locally in selector [OK]- Reversing global and local variable values
- Not using variable in property for local override
- Defining variables only inside buttons without global
