Challenge - 5 Problems
Sass @if Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Check the value of $theme and which @if branch matches it.
✗ Incorrect
The variable $theme is set to 'dark', so the @else if branch for 'dark' runs, setting background to black and color to white.
🧠 Conceptual
intermediate2:00remaining
Which @if condition is true in this Sass snippet?
Consider this Sass code:
Which condition will Sass evaluate as true?
$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%; } }
Attempts:
2 left
💡 Hint
Compare the numeric values with units in Sass.
✗ Incorrect
Sass can compare units like px. Since 10px is greater than 5px, the first condition is true and width: 100% is applied.
❓ selector
advanced2:00remaining
What CSS selector is generated by this Sass code with @if?
Look at this Sass code:
Which CSS selector will appear in the output?
$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; } } }
Attempts:
2 left
💡 Hint
Check how the & symbol works with nested selectors in Sass.
✗ Incorrect
The & symbol appends the nested class to the parent selector. Since $mode is 'dark', the .button.dark-mode selector is generated.
❓ layout
advanced2:00remaining
How does this Sass @if affect layout properties?
Given this Sass snippet:
What layout style will the container have?
$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; } }
Attempts:
2 left
💡 Hint
Boolean variables can be used directly in @if conditions in Sass.
✗ Incorrect
Since $is-flex is true, the @if branch sets display to flex and centers children horizontally with justify-content.
❓ accessibility
expert3: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; } }
Attempts:
2 left
💡 Hint
The lightness() function returns brightness; higher means lighter color.
✗ Incorrect
The code checks if background is light (lightness > 50%) and sets text color to black for readability; otherwise white for dark backgrounds.