📖 Scenario: You are creating a simple style sheet for a website that changes the background color of a button based on whether it is active or disabled. You will use boolean values and logic in Sass to control the button's style.
🎯 Goal: Build a Sass stylesheet that uses boolean variables and logical operations to set the background color of a button. The button should be green if active, gray if disabled, and blue otherwise.
📋 What You'll Learn
Create boolean variables for $is-active and $is-disabled with exact values.
Create a variable $default-color with the value #0000ff (blue).
Use an @if statement with boolean logic to set $button-color based on $is-active and $is-disabled.
Write a CSS rule for button that sets background-color to $button-color.
💡 Why This Matters
🌍 Real World
Boolean values and logic in Sass help you create dynamic styles that change based on conditions, like button states or themes.
💼 Career
Understanding boolean logic in Sass is important for front-end developers to write clean, maintainable, and responsive stylesheets.
Progress0 / 4 steps
1
Set up boolean variables
Create two boolean variables in Sass: $is-active set to true and $is-disabled set to false.
SASS
Hint
Use true and false keywords in Sass to assign boolean values.
2
Add default color variable
Create a variable called $default-color and set it to the hex color #0000ff (blue).
SASS
Hint
Use a hex color code to assign the default color.
3
Use boolean logic to set button color
Create a variable $button-color that uses @if logic: if $is-active is true, set $button-color to #00ff00 (green); else if $is-disabled is true, set it to #808080 (gray); otherwise, set it to $default-color.
SASS
Hint
Use @if, @else if, and @else to check boolean variables and assign colors.
4
Apply button background color
Write a CSS rule for the button element that sets its background-color property to the $button-color variable.
SASS
Hint
Use the button selector and set background-color to the variable.
Practice
(1/5)
1. In Sass, which of the following represents a Boolean value?
easy
A. 1
B. "true"
C. "false"
D. true
Solution
Step 1: Understand Sass Boolean values
In Sass, Boolean values are the keywords true and false, not strings or numbers.
Step 2: Identify the correct Boolean
true uses true as a keyword, which is the correct Boolean value in Sass.
Final Answer:
true -> Option D
Quick Check:
Boolean values in Sass are true or false [OK]
Hint: Look for keywords true or false without quotes [OK]
Common Mistakes:
Confusing strings "true" or "false" with Boolean values
Using numbers like 1 or 0 as Booleans
Thinking true/false must be quoted
2. Which of the following is the correct syntax to use a Boolean in an @if statement in Sass?
easy
A. @if $is-active == true { color: blue; }
B. @if $is-active = true { color: blue; }
C. @if $is-active === true { color: blue; }
D. @if $is-active != true { color: blue; }
Solution
Step 1: Check Sass comparison operator
Sass uses == to compare values in @if statements, not = or ===.
Step 2: Identify correct syntax
@if $is-active == true { color: blue; } uses == correctly to compare $is-active with true.