The @if directive lets you write conditional code in Sass. It runs certain styles only if a condition is true.
Click to reveal answer
beginner
How do you write an @if statement in Sass?
You write @if (condition) { /* styles here */ }. The styles inside run only if the condition is true.
Click to reveal answer
beginner
Can you use @else with @if in Sass? What does it do?
Yes. @else runs styles if the @if condition is false. It helps handle two cases clearly.
Click to reveal answer
beginner
Example: What will this Sass code output?
@if 5 > 3 { color: blue; }
Since 5 is greater than 3, the condition is true. The output CSS will be:
color: blue;
Click to reveal answer
intermediate
Why use @if in Sass instead of plain CSS?
CSS alone can't do conditions. Sass @if lets you write smarter styles that change based on variables or logic, saving time and code.
Click to reveal answer
What happens if the @if condition is false and there is no @else?
AThe styles inside <code>@if</code> are skipped.
BThe styles inside <code>@if</code> run anyway.
CSass throws an error.
DThe styles inside <code>@if</code> run twice.
✗ Incorrect
If the condition is false and no @else is provided, Sass skips the styles inside the @if block.
Which of these is a valid Sass @if condition?
A@if (5 > 3) { ... }
B@if color = blue { ... }
C@if (color == blue) { ... }
D@if (font-size > 12px) { ... }
✗ Incorrect
Sass supports numeric comparisons like 5 > 3. Option C is invalid because 'color' is undefined (though == is valid for equality), B uses = which is assignment, and D compares undefined 'font-size'.
What keyword do you use to add a fallback style if @if condition is false?
A@elseif
B@else
C@otherwise
D@fallback
✗ Incorrect
@else runs styles when the @if condition is false.
Can you nest @if statements inside each other in Sass?
ANo, nesting is not allowed.
BOnly one level of nesting is allowed.
CYes, nesting <code>@if</code> is allowed.
DOnly with <code>@else</code>.
✗ Incorrect
Sass allows nesting @if statements inside each other for complex conditions.
The first condition $theme == light is false, so it skips to @else if $theme == dark, which is true. So it applies background: black; and color: white;.
Final Answer:
body { background: black; color: white; } -> Option A
Quick Check:
$theme = dark triggers @else if dark block [OK]
Hint: Match variable value to @if conditions carefully [OK]
Common Mistakes:
Choosing first @if block without checking variable
B. Units like px cannot be compared directly in @if.
C. Comparison operators like > and < are not allowed in @if.
D. Variable $size should be declared inside the block.
Solution
Step 1: Understand unit comparison in Sass
Sass does not allow direct comparison of values with units like 10px > 5 in @if conditions.
Step 2: Check other options
Comparison operators are allowed, @else block is optional, and variables can be declared outside blocks.
Final Answer:
Units like px cannot be compared directly in @if. -> Option B
Quick Check:
Cannot compare px units directly in @if [OK]
Hint: Avoid comparing units directly; use numbers or functions [OK]
Common Mistakes:
Thinking > and < are invalid operators
Assuming @else is mandatory
Declaring variables only inside blocks
5. You want to set a button's background color based on a variable $status that can be 'success', 'warning', or 'error'. Which Sass code correctly uses @if conditional logic to apply green for success, yellow for warning, and red for error, with a default gray if none match?
In Sass, string values must be quoted and compared with ==, not = or ===.
Step 2: Verify the use of quotes around strings
Options A and B miss quotes or use wrong operators; @if $status === 'success' {
background: green;
} @else if $status === 'warning' {
background: yellow;
} @else if $status === 'error' {
background: red;
} @else {
background: gray;
} uses === which is invalid in Sass.