📖 Scenario: You are designing a simple webpage theme. You want to start with a base color and create lighter and darker versions of it for backgrounds and buttons.
🎯 Goal: Build a Sass stylesheet that defines a base color, sets a light and dark variant using the sass:color module functions, and applies these colors to page elements.
📋 What You'll Learn
Use the sass:color module functions lighten() and darken()
Create variables for base color, light color, and dark color
Apply these colors to CSS selectors for background and text
Use valid Sass syntax with module import
💡 Why This Matters
🌍 Real World
Web designers often need to create color themes with consistent variations for UI elements. Using sass:color functions helps automate this process.
💼 Career
Knowing how to manipulate colors programmatically with Sass is valuable for front-end developers and UI designers to build maintainable and scalable stylesheets.
Progress0 / 4 steps
1
Import the sass:color module and create a base color variable
Write @use "sass:color"; at the top. Then create a variable called $base-color and set it to #3498db (a blue shade).
SASS
Hint
Remember to use @use to import the module before using its functions.
2
Create light and dark color variables using sass:color functions
Create a variable $light-color by calling color.lighten($base-color, 20%). Create another variable $dark-color by calling color.darken($base-color, 20%).
SASS
Hint
Use the color.lighten() and color.darken() functions with the correct percentage.
3
Apply the colors to CSS selectors
Write CSS rules for body with background color $light-color, for h1 with color $dark-color, and for button with background color $base-color and white text color.
SASS
Hint
Use the variables inside the CSS rules exactly as named.
4
Add a hover effect to the button using sass:color.darken()
Add a button:hover rule that changes the background color to color.darken($base-color, 10%).
SASS
Hint
Use the color.darken() function inside the hover rule exactly as shown.
Practice
(1/5)
1. What does the lighten() function from the sass:color module do to a color?
easy
A. It makes the color darker by decreasing its brightness.
B. It makes the color lighter by increasing its brightness.
C. It changes the color to its complementary color.
D. It converts the color to grayscale.
Solution
Step 1: Understand the lighten() function purpose
The lighten() function increases the lightness of a color, making it appear lighter.
It makes the color lighter by increasing its brightness. -> Option B
Quick Check:
lighten() = increase brightness [OK]
Hint: Lighten means make brighter or closer to white [OK]
Common Mistakes:
Confusing lighten() with darken()
Thinking lighten() changes hue
Assuming lighten() converts to grayscale
2. Which of the following is the correct syntax to darken a color by 20% using the sass:color module?
easy
A. darken($color, 20%)
B. darken($color 20%)
C. darken($color, 0.2)
D. darken($color; 20%)
Solution
Step 1: Recall correct function syntax
The darken() function takes two arguments separated by a comma: the color and the amount as a percentage.
Step 2: Check each option
darken($color, 20%) uses comma and percentage correctly. Options B and D miss comma or use wrong separator. darken($color, 0.2) uses decimal instead of percentage.
Final Answer:
darken($color, 20%) -> Option A
Quick Check:
Function args separated by comma, amount as percentage [OK]
Hint: Use comma and % for amount in color functions [OK]
A. Using wrong function name; should be color.merge.
B. Percentage value should be a decimal, not 50%.
C. Missing comma between color and percentage in color.mix arguments.
D. The color #123456 is invalid.
Solution
Step 1: Check syntax of color.mix()
The color.mix() function requires two colors separated by a comma, and the weight percentage as a separate argument.
Step 2: Identify missing comma
In the code, #ffffff 50% lacks a comma between color and percentage, causing syntax error.
Final Answer:
Missing comma between color and percentage in color.mix arguments. -> Option C
Quick Check:
color.mix() args must be comma-separated [OK]
Hint: Separate all arguments with commas in functions [OK]
Common Mistakes:
Omitting commas between arguments
Using wrong function names
Misunderstanding percentage format
5. You want to create a button background color that is a 30% mix of your brand color $brand and white, but only if the brand color is dark. Which Sass code using sass:color module correctly applies this logic?
hard
A. @use "sass:color";
$bg: color.mix($brand, white, 30%);
B. @use "sass:color";
$bg: if(color.is-light($brand), color.mix($brand, white, 30%), $brand);
C. @use "sass:color";
$bg: if(color.is-dark($brand), color.mix(white, $brand, 30%), $brand);
D. @use "sass:color";
$bg: if(color.is-dark($brand), color.mix($brand, white, 30%), $brand);
Solution
Step 1: Understand the condition
You want to mix only if $brand is dark, so use color.is-dark($brand) as condition.
Step 2: Use correct mix order and percentage
Mix $brand with white at 30%. The color.mix() function takes base color first, then color to mix, then percentage.