Discover how controlling where your colors and sizes apply can save hours of frustrating bugs!
Variable scope (global vs local) in SASS - When to Use Which
Imagine you are writing styles for a website and you create a color variable at the top. Then inside a button style, you create another variable with the same name but a different color.
You want to use the button color inside the button only, but accidentally the whole site changes color because you didn't control where the variable applies.
Without understanding variable scope, you might overwrite colors everywhere or get unexpected results. You have to rename variables all the time or guess which color is used where. This makes your styles confusing and hard to fix.
Variable scope in Sass lets you decide if a variable is global (used everywhere) or local (used only inside a block). This way, you can safely reuse variable names without conflicts and keep your styles organized.
$color: blue;
.button {
$color: red !global;
color: $color;
}
body {
color: $color;
}$color: blue !global;
.button {
$color: red;
color: $color;
}
body {
color: $color;
}You can write clean, reusable styles where variables only affect the parts you want, avoiding accidental changes and confusion.
When styling a website, you might have a global primary color but want a special button to use a different shade without changing the whole site's color scheme.
Variable scope controls where variables apply in your styles.
Global variables affect the whole stylesheet; local variables only affect inside blocks.
Using scope prevents accidental overwrites and keeps code clear.
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
