0
0
SASSmarkup~20 mins

Variable scope (global vs local) in SASS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sass Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding variable scope in Sass
What will be the value of $color after compiling this Sass code?
SASS
$color: blue;

@mixin change-color() {
  $color: red;
}

@include change-color();

body {
  color: $color;
}
Aundefined variable error
Bred
Cblue
Dempty string
Attempts:
2 left
💡 Hint
Think about whether the variable inside the mixin changes the global variable or just a local copy.
📝 Syntax
intermediate
2:00remaining
Using !global to change variable scope
What will be the color of the p element after compiling this Sass code?
SASS
$font-size: 12px;

@mixin set-font() {
  $font-size: 16px !global;
}

@include set-font();

p {
  font-size: $font-size;
}
A16px
B12px
Cerror: !global not allowed here
D0px
Attempts:
2 left
💡 Hint
The !global flag changes the variable in the global scope.
rendering
advanced
2:00remaining
Effect of variable scope on nested selectors
Given this Sass code, what color will the .container .title text be in the compiled CSS?
SASS
$text-color: black;

.container {
  $text-color: green;
  .title {
    color: $text-color;
  }
}

.title {
  color: $text-color;
}
Agreen for .container .title, black for .title
Bblack for both .container .title and .title
Cgreen for both .container .title and .title
Derror: $text-color undefined in .title
Attempts:
2 left
💡 Hint
Variables inside a selector are local to that selector and its children.
selector
advanced
2:00remaining
Variable scope impact on @function output
What will be the output of colorize() function when called in this Sass code?
SASS
$base-color: blue;

@function colorize() {
  $base-color: red;
  @return $base-color;
}

.result {
  color: colorize();
}
Ablue
Berror: cannot redefine $base-color
Cundefined
Dred
Attempts:
2 left
💡 Hint
Variables inside functions are local unless marked global.
accessibility
expert
3:00remaining
Ensuring accessible color variables with global scope
You want to define a global color variable for accessible text contrast in Sass. Which approach correctly sets a global variable that can be used anywhere, including inside mixins and functions?
A$accessible-color: #000;
B$accessible-color: #000 !global;
C@function get-accessible-color() { $accessible-color: #000; @return $accessible-color; }
D@mixin set-accessible-color() { $accessible-color: #000; } @include set-accessible-color();
Attempts:
2 left
💡 Hint
Global variables must be declared with !global if set inside a block.