0
0
SASSmarkup~30 mins

Mixins with parameters in SASS - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Mixins with Parameters in Sass
📖 Scenario: You are designing a simple website and want to reuse a button style with different colors. Instead of writing the same CSS multiple times, you will use a Sass mixin with parameters to create buttons with different background colors.
🎯 Goal: Create a Sass mixin named button-style that accepts a $color parameter. Use this mixin to style two buttons with different background colors.
📋 What You'll Learn
Create a mixin named button-style with a $color parameter
The mixin should set the background color to $color, text color to white, padding to 1rem 2rem, and border-radius to 0.5rem
Use the mixin to style a button with class .btn-primary with background color #007bff
Use the mixin to style a button with class .btn-success with background color #28a745
💡 Why This Matters
🌍 Real World
Mixins with parameters are used in real projects to write reusable, customizable styles that save time and reduce errors.
💼 Career
Knowing how to write and use Sass mixins with parameters is a valuable skill for front-end developers working on scalable CSS codebases.
Progress0 / 4 steps
1
Create the button-style mixin with a $color parameter
Write a Sass mixin named button-style that takes one parameter called $color. Inside the mixin, set background-color to $color, color to white, padding to 1rem 2rem, and border-radius to 0.5rem.
SASS
Hint

Use @mixin to define a mixin and include the parameter $color in parentheses.

2
Style the .btn-primary button using the mixin with color #007bff
Write a CSS rule for the class .btn-primary. Inside it, include the button-style mixin with the argument #007bff.
SASS
Hint

Use @include followed by the mixin name and the color argument inside parentheses.

3
Style the .btn-success button using the mixin with color #28a745
Write a CSS rule for the class .btn-success. Inside it, include the button-style mixin with the argument #28a745.
SASS
Hint

Repeat the same pattern as for .btn-primary, but use the color #28a745.

4
Add a hover effect to both buttons using the mixin with a darker color
Inside both .btn-primary and .btn-success rules, add a :hover selector. Use the button-style mixin with #0056b3 for .btn-primary:hover and #1e7e34 for .btn-success:hover.
SASS
Hint

Use the nested &:hover selector inside each button class and include the mixin with the darker color.