📖 Scenario: You are creating a style sheet for a website. You want to reuse a calculation for colors to keep your code clean and easy to update.
🎯 Goal: Build a Sass file that defines a custom function to darken a color by a fixed amount, then use that function to style a button's background color.
📋 What You'll Learn
Create a custom function called darken-color that takes one color parameter
Inside the function, return the color darkened by 20%
Create a variable $base-color with the value #3498db
Use the darken-color function to set the background color of a button
💡 Why This Matters
🌍 Real World
Custom functions in Sass let you write reusable code for common style calculations, saving time and reducing mistakes.
💼 Career
Knowing how to write and use custom functions is important for front-end developers to create maintainable and scalable CSS.
Progress0 / 4 steps
1
Create the base color variable
Create a Sass variable called $base-color and set it to the color #3498db.
SASS
Hint
Use $base-color: #3498db; to create the variable.
2
Define the custom function darken-color
Write a custom Sass function called darken-color that takes one parameter named $color and returns the color darkened by 20%. Use the built-in darken() function inside your custom function.
SASS
Hint
Use @function darken-color($color) { @return darken($color, 20%); }.
3
Use the custom function to style a button
Create a CSS rule for button that sets its background-color to the result of calling darken-color with $base-color as the argument.
SASS
Hint
Use background-color: darken-color($base-color); inside the button rule.
4
Add a hover effect using the custom function
Add a :hover style for the button that sets the background-color to the original $base-color (not darkened).
SASS
Hint
Use &:hover { background-color: $base-color; } inside the button rule.
Practice
(1/5)
1. Why are custom functions useful in Sass?
easy
A. They make JavaScript run faster.
B. They let you reuse code and calculations easily.
C. They replace HTML tags.
D. They automatically fix CSS errors.
Solution
Step 1: Understand the purpose of custom functions
Custom functions in Sass allow you to write reusable code snippets that perform calculations or return values.
Step 2: Identify the benefit of reusing code
Reusing code helps keep styles consistent and easier to maintain, which is exactly what custom functions provide.
Final Answer:
They let you reuse code and calculations easily. -> Option B
Quick Check:
Custom functions = reuse code [OK]
Hint: Custom functions help reuse code and calculations [OK]
Common Mistakes:
Thinking custom functions fix CSS errors
Confusing Sass functions with HTML or JavaScript features
The function double multiplies the input by 2, so double(10px) returns 20px.
Step 2: Check the compiled CSS output
The Sass compiler replaces the function call with the returned value, so the margin becomes 20px.
Final Answer:
.box { margin: 20px; } -> Option D
Quick Check:
Function doubles 10px to 20px [OK]
Hint: Custom functions return values replacing calls in CSS [OK]
Common Mistakes:
Expecting function name to appear in CSS
Thinking Sass functions run in browser
Confusing input and output units
4. Identify the error in this Sass custom function:
@function add($a, $b) { return $a + $b; }
medium
A. Using 'return' instead of '@return' causes error
B. Function name cannot be 'add'
C. Missing @return keyword inside the function
D. Parameters must be declared without $
Solution
Step 1: Check Sass function syntax
Sass requires @return keyword to return values, not plain return.
Step 2: Identify the error
Using return without @ causes a syntax error in Sass.
Final Answer:
Using 'return' instead of '@return' causes error -> Option A
Quick Check:
@return required, not return [OK]
Hint: Use @return, not return, inside Sass functions [OK]
Common Mistakes:
Writing 'return' without @ in Sass
Thinking function names are restricted
Omitting $ in parameter names
5. You want to create a custom function in Sass that returns a color with adjusted opacity. Which approach best uses custom functions to keep your design consistent?
hard
A. Use JavaScript to change opacity after the page loads.
B. Write the rgba color manually every time you need opacity.
C. Define a function that takes a color and opacity, then returns the color with that opacity applied.
D. Create multiple CSS classes for each opacity level.
Solution
Step 1: Understand the goal of consistency and reuse
Using a custom function to adjust opacity means you write the logic once and reuse it everywhere.
Step 2: Compare options for maintainability
Manually writing rgba or using JS or many classes is less maintainable and less consistent than a function.
Final Answer:
Define a function that takes a color and opacity, then returns the color with that opacity applied. -> Option C
Quick Check:
Custom function for opacity = consistent design [OK]
Hint: Use functions to apply opacity consistently [OK]
Common Mistakes:
Hardcoding colors repeatedly
Relying on JavaScript for style changes
Creating many CSS classes instead of functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.