📖 Scenario: You are creating a simple webpage style using SASS to make your CSS easier to manage and read.
🎯 Goal: Build a SASS stylesheet that defines a color variable, uses it for a background color, and styles a heading with a font size and color.
📋 What You'll Learn
Create a SASS variable for a primary color
Use the variable to set the background color of the body
Style an h1 heading with a font size and color
Use nested syntax to style the h1 inside the body
💡 Why This Matters
🌍 Real World
SASS is used in real websites to write CSS faster and keep styles organized.
💼 Career
Knowing SASS is valuable for front-end developers to maintain large style codebases efficiently.
Progress0 / 4 steps
1
Create a SASS variable for the primary color
Write a SASS variable called $primary-color and set it to #3498db.
SASS
Hint
Variables in SASS start with a dollar sign $.
2
Set the body background color using the variable
Write a body selector and set its background-color property to the $primary-color variable.
SASS
Hint
Use the variable by writing its name with the dollar sign inside the property value.
3
Add styles for the h1 inside the body using nesting
Inside the body selector, nest an h1 selector. Set its font-size to 2rem and color to white.
SASS
Hint
Use nested syntax by writing the h1 selector inside the body block.
4
Complete the SASS stylesheet
Make sure the SASS code includes the $primary-color variable, the body selector with background-color, and the nested h1 with font-size and color properties.
SASS
Hint
Review your code to ensure all parts are included exactly as instructed.
Practice
(1/5)
1. What is the main benefit of using variables in a SASS stylesheet?
easy
A. They automatically fix CSS errors.
B. They make the stylesheet load faster in the browser.
C. They let you reuse values like colors and sizes easily.
D. They convert SASS to JavaScript.
Solution
Step 1: Understand what variables do in SASS
Variables store values such as colors or sizes so you can reuse them without repeating the same value.
Step 2: Compare options with this understanding
Only They let you reuse values like colors and sizes easily. correctly describes this benefit. Other options describe unrelated or incorrect features.
Final Answer:
They let you reuse values like colors and sizes easily. -> Option C
Quick Check:
Variables store reusable values = A [OK]
Hint: Variables store reusable values like colors or sizes [OK]
Common Mistakes:
Thinking variables speed up browser loading
Believing variables fix CSS errors automatically
Confusing SASS variables with JavaScript
2. Which of the following is the correct way to declare a variable in SASS?
easy
A. $main-color: #3498db;
B. var main-color = #3498db;
C. main-color: #3498db;
D. @main-color: #3498db;
Solution
Step 1: Recall SASS variable syntax
In SASS, variables start with a dollar sign ($) followed by the name, then a colon and the value.
Step 2: Match options to this syntax
$main-color: #3498db; matches this exactly. Options B, C, and D use incorrect syntax.
In SASS, each statement must end with a semicolon. The variable declaration lacks a semicolon.
Step 2: Verify other parts
Variable names start with $, font-size is valid, and body selector is allowed.
Final Answer:
Missing semicolon after variable declaration -> Option B
Quick Check:
Semicolon missing after $font-size declaration = D [OK]
Hint: Always end variable declarations with semicolon [OK]
Common Mistakes:
Omitting semicolon after variable
Thinking $ is invalid for variables
Assuming selectors can't be used
5. You want to create a SASS stylesheet where all buttons have a background color stored in a variable, and on hover, the background becomes 15% lighter. Which SASS code correctly achieves this?
$btn-bg: #007bff;
button {
background-color: $btn-bg;
&:hover {
background-color: lighten($btn-bg, 15%);
}
} correctly declares $btn-bg with a semicolon and uses proper nesting with &:hover.
Step 2: Verify color function usage
$btn-bg: #007bff;
button {
background-color: $btn-bg;
&:hover {
background-color: lighten($btn-bg, 15%);
}
} uses lighten($btn-bg, 15%) to make the color lighter on hover, which matches the requirement.