Variable scope tells us where a variable can be used in our styles. It helps keep styles organized and avoid mistakes.
Variable scope (global vs local) in SASS
$variable-name: value; // global @mixin example { $variable-name: new-value !global; // change global variable inside mixin } .selector { $variable-name: local-value; // local variable inside selector }
Variables declared outside any block are global by default.
Use !global to change a global variable inside a nested block.
$color inside a selector.$color: blue; .button { color: $color; // uses global blue }
.button, $color is local and red. Outside, it stays blue.$color: blue; .button { $color: red; color: $color; // uses local red } .alert { color: $color; // uses global blue }
$color to green using !global.$color: blue; @mixin change-color { $color: green !global; } @include change-color; .alert { color: $color; // now green }
This code shows two boxes. The .button uses a local $main-color of red. The .alert uses the global $main-color which is blue.
$main-color: blue; .button { $main-color: red; background-color: $main-color; color: white; padding: 1rem; border-radius: 0.5rem; } .alert { background-color: $main-color; color: white; padding: 1rem; border-radius: 0.5rem; }
Local variables only exist inside the block where they are declared.
Global variables can be read anywhere but to change them inside a block, use !global.
Using scopes helps avoid accidentally changing variables used in other parts of your styles.
Global variables are set outside blocks and can be used anywhere.
Local variables are set inside blocks and only work there.
Use !global to update a global variable inside a nested block.
Practice
$color: red; declared outside any block means:Solution
Step 1: Understand where the variable is declared
The variable$color: red;is declared outside any selector or block, making it global.Step 2: Recall Sass variable scope rules
Global variables can be accessed anywhere in the stylesheet, inside or outside blocks.Final Answer:
The variable is global and can be used anywhere in the stylesheet. -> Option CQuick Check:
Global variable outside blocks = accessible everywhere [OK]
- Thinking variables outside blocks are local
- Confusing !global with !local
- Assuming variables need special flags to be global
$font-size inside a nested block?Solution
Step 1: Identify how to update global variables inside blocks
To update a global variable inside a nested block, you must add!globalafter the value.Step 2: Check each option's syntax
Inside block:$font-size: 2rem !global;uses$font-size: 2rem !global;which is correct Sass syntax for updating global variables inside blocks.Final Answer:
Inside block: $font-size: 2rem !global; -> Option DQuick Check:
Use !global to update global variables inside blocks [OK]
- Forgetting !global and only assigning value locally
- Using !local which is invalid
- Placing !global before variable name
p text be in this Sass code?$color: blue;
p {
$color: red;
color: $color;
}Solution
Step 1: Identify variable scopes
$color is set globally to blue, but insidepblock, a local $color is set to red.Step 2: Determine which $color is used in
Inside the block, the local $color (red) overrides the global one.color: $color;Final Answer:
Red -> Option BQuick Check:
Local variable overrides global inside blocks [OK]
- Assuming global variable is used inside block
- Thinking Sass throws error for local variable
- Confusing inheritance with variable scope
$padding: 10px;
.container {
$padding: 20px;
padding: $padding;
}Solution
Step 1: Analyze variable update inside block
The code declares$padding: 20px;inside.containerblock without!global, so it creates a local variable instead of updating the global one.Step 2: Identify correct way to update global variable
To update the global$paddinginside the block,!globalmust be added:$padding: 20px !global;.Final Answer:
Missing !global to update the global variable inside the block. -> Option AQuick Check:
Use !global to update global variables inside blocks [OK]
- Assuming local assignment updates global variable
- Thinking variables can't be used inside blocks
- Confusing variable declaration syntax
$margin after compilation?$margin: 5px;
.wrapper {
$margin: 10px !global;
.content {
$margin: 15px;
}
}
body {
margin: $margin;
}Solution
Step 1: Track global variable changes
Initially,$marginis 5px globally. Inside.wrapper,$margin: 10px !global;updates the global variable to 10px.Step 2: Check nested block
Inside.content.content,$margin: 15px;is local to that block and does not affect the global variable.Step 3: Determine value used in
Thebodybodyselector uses the global$margin, which was updated to 10px.Final Answer:
10px -> Option AQuick Check:
!global updates global variable; local inside nested blocks doesn't [OK]
- Assuming nested local variable changes global
- Confusing variable values inside nested blocks
- Expecting compilation error due to variable shadowing
Ready to prove it?
Pass the test and this pattern moves unknown
