📖 Scenario: You are creating a simple webpage with buttons that share common styles. Instead of repeating the same CSS rules, you want to use Sass's @extend directive to reuse styles efficiently.
🎯 Goal: Build Sass code that uses the @extend directive to share styles between button classes, reducing repetition and making the CSS easier to maintain.
📋 What You'll Learn
Create a base button style class called .btn with background color, padding, and border radius.
Create a secondary button class called .btn-secondary that extends .btn and adds a different background color.
Create a danger button class called .btn-danger that extends .btn and adds a red background color.
Use the @extend directive exactly as specified to share styles.
💡 Why This Matters
🌍 Real World
Using @extend helps keep CSS code DRY (Don't Repeat Yourself) and easier to maintain when multiple elements share common styles.
💼 Career
Front-end developers often use Sass features like @extend to write cleaner, more efficient stylesheets for websites and web apps.
Progress0 / 4 steps
1
Create the base button style
Create a Sass class called .btn with these exact styles: background-color: #007bff;, padding: 0.5rem 1rem;, and border-radius: 0.25rem;.
SASS
Hint
Use the .btn selector and add the styles inside curly braces.
2
Add the secondary button class
Create a Sass class called .btn-secondary that uses @extend .btn; and adds background-color: #6c757d;.
SASS
Hint
Use @extend .btn; inside the .btn-secondary class to reuse styles.
3
Add the danger button class
Create a Sass class called .btn-danger that uses @extend .btn; and adds background-color: #dc3545;.
SASS
Hint
Use @extend .btn; inside the .btn-danger class to reuse styles.
4
Complete with a hover effect on base button
Add a hover effect to the .btn class using &:hover that changes background-color to #0056b3.
SASS
Hint
Use &:hover inside .btn to add the hover style.
Practice
(1/5)
1. What does the @extend directive do in Sass?
easy
A. It defines a variable.
B. It creates a new CSS property.
C. It imports external CSS files.
D. It allows one selector to reuse styles from another selector.
Solution
Step 1: Understand the purpose of @extend
The @extend directive is used to share styles between selectors in Sass.
Step 2: Identify what @extend does
It copies the styles of one selector into another, reducing repetition.
Final Answer:
It allows one selector to reuse styles from another selector. -> Option D
Quick Check:
@extend = reuse styles [OK]
Hint: Remember: @extend shares styles between selectors [OK]
Common Mistakes:
Thinking @extend creates new properties
Confusing @extend with imports
Using @extend to define variables
2. Which of the following is the correct syntax to extend styles from .button to .submit in Sass?
easy
A. .submit { @extend .button; }
B. .submit { extend(@button); }
C. .submit { @extend .button(); }
D. .submit { @extend '#button'; }
Solution
Step 1: Recall correct @extend syntax
The correct syntax is @extend followed by the selector name without quotes or parentheses.
Step 2: Check each option
.submit { @extend .button; } uses @extend .button; which is correct. Others use wrong syntax like parentheses or quotes.
Final Answer:
.submit { @extend .button; } -> Option A
Quick Check:
Correct syntax = @extend .selector; [OK]
Hint: Use @extend followed by selector without quotes or parentheses [OK]
The selector to extend must include the dot for class selectors.
Step 2: Identify error in code
@extend card; misses the dot, should be @extend .card;.
Final Answer:
Missing dot before card in @extend selector. -> Option A
Quick Check:
Class selectors need dot in @extend [OK]
Hint: Always include dot for class names in @extend [OK]
Common Mistakes:
Omitting dot before class name
Thinking @extend can't be used inside rules
Confusing property errors with selector errors
5. You want to create a .warning style that shares all styles from .alert but also adds background-color: yellow;. Which Sass code correctly does this?
hard
A. .warning { @extend alert; background-color: yellow; }
B. .warning { background-color: yellow; @extend .alert(); }
C. .warning { @extend .alert; background-color: yellow; }
D. .warning { @extend .alert background-color: yellow; }
Solution
Step 1: Understand @extend usage with additional styles
You can use @extend inside a selector and add other properties after it.
Step 2: Check each option
.warning { @extend .alert; background-color: yellow; } correctly uses @extend .alert; then adds background-color in the same block. Others have syntax errors: parentheses after selector, missing dot before alert, or missing semicolon.
Final Answer:
.warning { @extend .alert; background-color: yellow; } -> Option C
Quick Check:
@extend plus extra styles in same block [OK]
Hint: Put @extend first, then add extra styles in same block [OK]
Common Mistakes:
Adding parentheses after selector
Omitting dot before class name in @extend
Missing semicolon after @extend
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.