SASS/SCSS / Inheritance and Extends
Using @extend for Inheritance in SASS/SCSS
In this tutorial, you'll learn how to use @extend in SASS/SCSS to inherit styles from another selector. This can help you write more efficient and maintainable stylesheets.
Section overview
5 resourcesCovers how to reuse styles with inheritance and @extend in SASS/SCSS.
SASS/SCSS Tutorial: Using @extend for Inheritance
1. Introduction
In this tutorial, we will be focusing on how to use the @extend directive in SASS/SCSS for creating efficient stylesheets by inheriting styles from another selector. This is a powerful feature that can help you avoid code repetition and make your stylesheets more maintainable and readable.
By the end of this tutorial, you should be able to:
- Understand how the @extend directive works
- Use @extend to inherit styles from other selectors
- Apply best practices when using @extend
Prerequisites: Basic knowledge of CSS and understanding of SASS/SCSS.
2. Step-by-Step Guide
The @extend directive in SASS/SCSS is used to inherit the styles of one selector from another. This is similar to how classes can inherit properties and methods from another class in object-oriented programming.
Best Practices and Tips:
- Avoid extending complex selectors, as it can lead to unintended styling and make your stylesheet difficult to maintain.
- Use @extend with placeholder selectors (%). They are not output to CSS unless extended, so they can be a good way to group common styles without bloating your CSS.
3. Code Examples
Example 1: Basic Usage of @extend
.error {
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
@extend .error;
border-width: 3px;
}
In this example, .seriousError will inherit the styles from .error and then add its own styles. The generated CSS will look like this:
.error, .seriousError {
border: 1px #f00;
background-color: #fdd;
}
.seriousError {
border-width: 3px;
}
Example 2: Using @extend with placeholder selectors
%button {
display: inline-block;
padding: 5px 10px;
border-radius: 3px;
text-align: center;
}
.primary-button {
@extend %button;
background-color: #007bff;
color: white;
}
.secondary-button {
@extend %button;
background-color: #6c757d;
color: white;
}
In this example, .primary-button and .secondary-button both extend the %button placeholder selector. The generated CSS will look like this:
.primary-button, .secondary-button {
display: inline-block;
padding: 5px 10px;
border-radius: 3px;
text-align: center;
}
.primary-button {
background-color: #007bff;
color: white;
}
.secondary-button {
background-color: #6c757d;
color: white;
}
4. Summary
In this tutorial, we learned how to use the @extend directive in SASS/SCSS to inherit styles from another selector. We have seen how @extend can make your stylesheets more efficient and maintainable, and we've also covered some best practices when using @extend.
Next steps for learning include exploring other SASS/SCSS features such as mixins, variables, and functions.
Additional resources:
- SASS/SCSS documentation
5. Practice Exercises
-
Create a basic stylesheet with a few selectors, and try using @extend to inherit styles. Experiment with different combinations of selectors and properties.
-
Create a stylesheet using @extend with placeholder selectors. Try to extend multiple placeholder selectors in one selector.
-
Create a complex stylesheet with nested selectors, and use @extend to inherit styles across different levels of nesting.
Solutions and explanations for these exercises will depend on your creativity, but remember the best practices covered in this tutorial. For further practice, consider refactoring an existing stylesheet to use @extend where possible.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article