📖 Scenario: You are creating styles for a website with multiple buttons that share common styles but differ slightly in color.
🎯 Goal: Build a Sass stylesheet that uses a mixin to avoid repeating button styles and apply it to different button classes with unique colors.
📋 What You'll Learn
Create a mixin named button-style that sets padding, border-radius, and font-size
Create a variable named $primary-color with the value #3498db
Use the button-style mixin inside a .btn-primary class and set its background color to $primary-color
Use the button-style mixin inside a .btn-secondary class and set its background color to #2ecc71
💡 Why This Matters
🌍 Real World
Web designers often style many buttons or components with similar styles but different colors or sizes. Mixins help keep the code DRY (Don't Repeat Yourself).
💼 Career
Knowing how to use Sass mixins is important for front-end developers to write efficient, maintainable CSS for large projects.
Progress0 / 4 steps
1
Create the button-style mixin
Write a Sass mixin called button-style that sets padding to 1rem 2rem, border-radius to 0.5rem, and font-size to 1.25rem.
SASS
Hint
Use @mixin button-style { ... } and inside set the three CSS properties.
2
Create the $primary-color variable
Create a Sass variable named $primary-color and assign it the color value #3498db.
SASS
Hint
Use $primary-color: #3498db; to create the variable.
3
Use the mixin in .btn-primary with $primary-color
Create a CSS class .btn-primary that includes the button-style mixin and sets background-color to $primary-color.
SASS
Hint
Use .btn-primary { @include button-style; background-color: $primary-color; }.
4
Use the mixin in .btn-secondary with a different color
Create a CSS class .btn-secondary that includes the button-style mixin and sets background-color to #2ecc71.
SASS
Hint
Use .btn-secondary { @include button-style; background-color: #2ecc71; }.
Practice
(1/5)
1. Why do Sass mixins help reduce duplication in your stylesheets?
easy
A. Because they generate random colors for your styles
B. Because they let you write a style once and reuse it multiple times
C. Because they replace all CSS selectors with shorter names
D. Because they automatically compress your CSS files
Solution
Step 1: Understand what duplication means in CSS
Duplication means writing the same style code multiple times in different places.
Step 2: See how mixins solve duplication
Mixins let you write a style block once and reuse it by calling the mixin wherever needed.
Final Answer:
Because they let you write a style once and reuse it multiple times -> Option B
Quick Check:
Mixins reduce duplication = reuse styles [OK]
Hint: Mixins = write once, use many times [OK]
Common Mistakes:
Thinking mixins compress CSS files
Confusing mixins with CSS selectors
Believing mixins generate random styles
2. Which of the following is the correct syntax to define a mixin in Sass?
easy
A. @mixin example { color: red; }
B. @define example { color: red; }
C. @include example { color: red; }
D. @style example { color: red; }
Solution
Step 1: Recall the keyword to define a mixin
The correct keyword to define a mixin is @mixin.
Step 2: Check the syntax format
The mixin is defined with @mixin name { ... }. Other options use incorrect keywords.
Mixins must be called with @include mixin-name; inside selectors.
Step 2: Identify the error in usage
The code uses text-style; without @include, so it won't apply the mixin styles.
Final Answer:
Mixin is not called with @include keyword -> Option A
Quick Check:
Call mixins with @include [OK]
Hint: Always use @include to call mixins [OK]
Common Mistakes:
Forgetting @include before mixin name
Adding parameters when none needed
Placing mixin inside selector definition
5. You want to create a button style that can have different background colors but share the same padding and border radius. How can mixins help you avoid duplication?