📖 Scenario: You are designing a button style that changes color based on a theme variable. You want to use Sass @if conditional logic to set the button background color depending on whether the theme is 'dark' or 'light'.
🎯 Goal: Build a Sass stylesheet that uses @if conditional logic to set the button background color to #333 for dark theme and #eee for light theme.
📋 What You'll Learn
Create a Sass variable $theme with the value 'dark'.
Create a Sass variable $button-bg that uses @if to set background color based on $theme.
Use @if to check if $theme is 'dark' and assign #333 to $button-bg.
Use @else to assign #eee to $button-bg if $theme is not 'dark'.
Create a CSS rule for button that sets background-color to $button-bg.
💡 Why This Matters
🌍 Real World
Web developers often need to create themes or styles that change based on user preferences or settings. Using Sass @if conditional logic helps write clean, maintainable styles that adapt easily.
💼 Career
Knowing how to use Sass conditionals is valuable for front-end developers working on scalable CSS codebases, improving efficiency and reducing repetition.
Progress0 / 4 steps
1
Set up the theme variable
Create a Sass variable called $theme and set it to the string 'dark'.
SASS
Hint
Use $theme: 'dark'; to create the variable.
2
Declare the button background variable
Create a Sass variable called $button-bg but do not assign a value yet.
SASS
Hint
Declare $button-bg with null to initialize it.
3
Use @if to set $button-bg based on $theme
Write an @if statement that checks if $theme equals 'dark'. If true, set $button-bg to #333. Use @else to set $button-bg to #eee if $theme is not 'dark'.
SASS
Hint
Use @if $theme == 'dark' { $button-bg: #333; } and @else { $button-bg: #eee; }.
4
Create button style using $button-bg
Write a CSS rule for button that sets background-color to the Sass variable $button-bg.
SASS
Hint
Use button { background-color: $button-bg; } to style the button.
Practice
(1/5)
1. What does the @if directive do in Sass?
easy
A. It defines a variable.
B. It loops through a list of items.
C. It imports other Sass files.
D. It applies styles only if a condition is true.
Solution
Step 1: Understand the purpose of @if
The @if directive checks a condition and applies styles only when that condition is true.
Step 2: Compare with other options
Looping is done with @for or @each, importing with @import, and variables are defined with $.
Final Answer:
It applies styles only if a condition is true. -> Option D
Quick Check:
@if = conditional style application [OK]
Hint: Remember: @if controls styles by conditions [OK]
Common Mistakes:
Confusing @if with loops or imports
Thinking @if defines variables
Assuming @if always runs styles
2. Which of the following is the correct syntax for using @if in Sass?
easy
A. @if $color == red { color: red; }
B. @if ($color = red) { color: red; }
C. @if $color === red { color: red; }
D. @if $color => red { color: red; }
Solution
Step 1: Check the correct comparison operator
Sass uses == to compare values in @if conditions, not =, ===, or =>.
Step 2: Verify syntax structure
The correct syntax is @if $variable == value { ... } without parentheses around the condition.
Final Answer:
@if $color == red { color: red; } -> Option A
Quick Check:
Use == for comparison in @if [OK]
Hint: Use double equals (==) for conditions in @if [OK]
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.