📖 Scenario: You are creating a simple style sheet for a website. You want to learn how Sass handles null values when setting variables and using them in styles.
🎯 Goal: Build a Sass file that defines variables with null values, uses conditional logic to check for null, and applies styles accordingly.
📋 What You'll Learn
Create a Sass variable with a null value
Create a helper variable to check if the first variable is null
Use an @if statement to apply a style only if the variable is not null
Add a CSS rule that uses the variable or a fallback value
💡 Why This Matters
🌍 Real World
Handling null values in Sass helps you write flexible styles that adapt based on whether variables are set or not, useful in theming and design systems.
💼 Career
Understanding null behavior in Sass is important for front-end developers working with CSS preprocessors to create maintainable and dynamic stylesheets.
Progress0 / 4 steps
1
Create a Sass variable with a null value
Create a Sass variable called $primary-color and set it to null.
SASS
Hint
Use $primary-color: null; to assign a null value.
2
Create a helper variable to check for null
Create a Sass variable called $has-primary-color and set it to the result of checking if $primary-color is not null using if($primary-color != null, true, false).
SASS
Hint
Use the if() function to check if $primary-color is not null.
3
Use @if to apply style only if variable is not null
Write an @if statement that checks if $has-primary-color is true, and inside it set the CSS selector .button to have background-color: $primary-color;.
SASS
Hint
Use @if $has-primary-color { .button { background-color: $primary-color; } } to conditionally apply the style.
4
Add a CSS rule with fallback color
Add a CSS rule for the selector .button that sets color to $primary-color if it is not null, otherwise use #000000 as fallback using the if() function.
SASS
Hint
Use color: if($primary-color != null, $primary-color, #000000); inside .button.
Practice
(1/5)
1. What does null represent in Sass?
easy
A. A zero number
B. A false boolean
C. An empty string
D. No value or absence of a value
Solution
Step 1: Understand Sass null meaning
In Sass, null means no value is assigned, like an empty box.
Step 2: Differentiate from other values
Zero, empty string, and false are actual values, but null means nothing is there.
Final Answer:
No value or absence of a value -> Option D
Quick Check:
Null = no value [OK]
Hint: Null means no value, not zero or empty [OK]
Common Mistakes:
Confusing null with zero
Thinking null is an empty string
Assuming null is false
2. Which Sass syntax correctly checks if a variable $color is not null?
easy
A. @if $color = null { color: $color; }
B. @if $color != null { color: $color; }
C. @if $color == false { color: $color; }
D. @if $color !== null { color: $color; }
Solution
Step 1: Identify correct comparison operator
Sass uses != to check if a value is not equal to another.
Step 2: Check syntax correctness
@if $color != null { ... } is valid. = is assignment, not comparison. !== is not valid Sass syntax.
Final Answer:
@if $color != null { color: $color; } -> Option B
Quick Check:
Use != to check not null [OK]
Hint: Use != to check not null in Sass [OK]
Common Mistakes:
Using = instead of != for comparison
Using JavaScript-style !== which Sass doesn't support