📖 Scenario: You are creating a simple webpage style where multiple buttons share common styles. To avoid repeating code, you will use a Sass mixin and include it in different button classes.
🎯 Goal: Build a Sass stylesheet that defines a mixin called button-style with specific styles, then include this mixin in two button classes: .primary-button and .secondary-button.
📋 What You'll Learn
Create a mixin named button-style that sets padding to 1rem 2rem, border-radius to 0.5rem, and font-weight to bold.
Create a class .primary-button that includes the button-style mixin and sets background-color to #007bff and color to white.
Create a class .secondary-button that includes the button-style mixin and sets background-color to #6c757d and color to white.
Use the @include directive to include the mixin inside the button classes.
💡 Why This Matters
🌍 Real World
Mixins help keep CSS code clean and reusable, especially when many elements share similar styles like buttons.
💼 Career
Knowing how to use Sass mixins is valuable for front-end developers to write maintainable and scalable stylesheets.
Progress0 / 4 steps
1
Create the button-style mixin
Write a Sass mixin named button-style that sets padding to 1rem 2rem, border-radius to 0.5rem, and font-weight to bold.
SASS
Hint
Use @mixin button-style { ... } to define the mixin with the given styles inside.
2
Create the .primary-button class and include the mixin
Create a Sass class selector .primary-button that uses @include button-style to add the mixin styles, then set background-color to #007bff and color to white.
SASS
Hint
Use .primary-button { @include button-style; background-color: #007bff; color: white; }.
3
Create the .secondary-button class and include the mixin
Create a Sass class selector .secondary-button that uses @include button-style to add the mixin styles, then set background-color to #6c757d and color to white.
SASS
Hint
Use .secondary-button { @include button-style; background-color: #6c757d; color: white; }.
4
Add a hover effect to both buttons using the mixin
Add a hover style for both .primary-button and .secondary-button that changes the opacity to 0.8. Use the &:hover selector inside each button class.
SASS
Hint
Inside each button class, add &:hover { opacity: 0.8; } to create the hover effect.
Practice
(1/5)
1. What does the @include directive do in Sass?
easy
A. It inserts the styles defined in a mixin into the current selector.
B. It defines a new mixin with reusable styles.
C. It imports another Sass file into the current file.
D. It creates a variable to store a color or value.
Solution
Step 1: Understand the role of @include
The @include directive is used to add the styles from a mixin into a selector.
Step 2: Differentiate from other Sass features
Defining mixins uses @mixin, importing files uses @import, and variables use $ syntax.
Final Answer:
It inserts the styles defined in a mixin into the current selector. -> Option A
B. Mixin should be defined with parentheses even if no arguments.
C. Mixin name should start with $ sign.
D. Missing parentheses after mixin name in @include.
Solution
Step 1: Check @include syntax
When including a mixin, parentheses are required even if no arguments are passed.
Step 2: Verify other parts
Mixin definition without parentheses is allowed if no arguments. Mixin names do not start with $. @include is valid inside selectors.
Final Answer:
Missing parentheses after mixin name in @include. -> Option D
Quick Check:
@include needs parentheses even if empty [OK]
Hint: Always add () after mixin name in @include [OK]
Common Mistakes:
Omitting parentheses in @include
Thinking mixin names start with $
Believing @include can't be inside selectors
5. You want to create a reusable mixin card that sets a white background, shadow, and border radius. You also want to customize the border radius when including it. Which Sass code correctly defines and uses this mixin?