0
0
SASSmarkup~20 mins

@if conditional logic in SASS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sass @if Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the output CSS of this Sass code?
Given the Sass code below, what CSS will it produce?
$theme: dark;

body {
  @if $theme == light {
    background: white;
    color: black;
  } @else if $theme == dark {
    background: black;
    color: white;
  } @else {
    background: gray;
    color: black;
  }
}
SASS
$theme: dark;

body {
  @if $theme == light {
    background: white;
    color: black;
  } @else if $theme == dark {
    background: black;
    color: white;
  } @else {
    background: gray;
    color: black;
  }
}
A
body {
  background: transparent;
  color: inherit;
}
B
body {
  background: white;
  color: black;
}
C
body {
  background: black;
  color: white;
}
D
body {
  background: gray;
  color: black;
}
Attempts:
2 left
💡 Hint
Check the value of $theme and which @if branch matches it.
🧠 Conceptual
intermediate
2:00remaining
Which @if condition is true in this Sass snippet?
Consider this Sass code:
$size: 10px;

.container {
  @if $size > 5px {
    width: 100%;
  } @else {
    width: 50%;
  }
}

Which condition will Sass evaluate as true?
SASS
$size: 10px;

.container {
  @if $size > 5px {
    width: 100%;
  } @else {
    width: 50%;
  }
}
A$size > 5px is false, so width: 50% is applied.
B$size > 5px is true, so width: 100% is applied.
C$size > 5px causes a syntax error in Sass.
DSass ignores the condition and applies both widths.
Attempts:
2 left
💡 Hint
Compare the numeric values with units in Sass.
selector
advanced
2:00remaining
What CSS selector is generated by this Sass code with @if?
Look at this Sass code:
$mode: dark;

.button {
  @if $mode == dark {
    &.dark-mode {
      background: black;
      color: white;
    }
  } @else {
    &.light-mode {
      background: white;
      color: black;
    }
  }
}

Which CSS selector will appear in the output?
SASS
$mode: dark;

.button {
  @if $mode == dark {
    &.dark-mode {
      background: black;
      color: white;
    }
  } @else {
    &.light-mode {
      background: white;
      color: black;
    }
  }
}
A.button.dark-mode { background: black; color: white; }
B.button { background: black; color: white; }
C.button.light-mode { background: white; color: black; }
D.dark-mode { background: black; color: white; }
Attempts:
2 left
💡 Hint
Check how the & symbol works with nested selectors in Sass.
layout
advanced
2:00remaining
How does this Sass @if affect layout properties?
Given this Sass snippet:
$is-flex: true;

.container {
  @if $is-flex {
    display: flex;
    justify-content: center;
  } @else {
    display: block;
  }
}

What layout style will the container have?
SASS
$is-flex: true;

.container {
  @if $is-flex {
    display: flex;
    justify-content: center;
  } @else {
    display: block;
  }
}
AThe container will use block layout and ignore justify-content.
BThe container will cause a Sass error because $is-flex is not a string.
CThe container will have no display property set.
DThe container will use flex layout and center its children horizontally.
Attempts:
2 left
💡 Hint
Boolean variables can be used directly in @if conditions in Sass.
accessibility
expert
3:00remaining
Which Sass @if usage best supports accessible color contrast?
You want to ensure text color has enough contrast on different backgrounds. Which Sass code snippet correctly uses @if to set accessible colors?
$background: #333333;

.text {
  @if lightness($background) > 50% {
    color: black;
  } @else {
    color: white;
  }
}
SASS
$background: #333333;

.text {
  @if lightness($background) > 50% {
    color: black;
  } @else {
    color: white;
  }
}
ASets text color to white on dark backgrounds and black on light backgrounds, ensuring contrast.
BAlways sets text color to black regardless of background brightness.
CUses an invalid function lightness() causing a Sass error.
DSets text color to white on light backgrounds and black on dark backgrounds, reducing contrast.
Attempts:
2 left
💡 Hint
The lightness() function returns brightness; higher means lighter color.