SASS/SCSS / Inheritance and Extends
Best Practices for Inheritance in SASS/SCSS
In this tutorial, you'll learn the best practices for using inheritance in SASS/SCSS. This includes using @extend to inherit styles, avoiding code duplication, and understanding t…
Section overview
5 resourcesCovers how to reuse styles with inheritance and @extend in SASS/SCSS.
Best Practices for Inheritance in SASS/SCSS
1. Introduction
In this tutorial, we'll go over the best practices for using inheritance in SASS/SCSS. Inheritance allows you to share a set of CSS properties from one selector to another. We'll specifically focus on using the @extend directive to inherit styles, how to avoid code duplication, and understanding the limitations of @extend.
By the end of this tutorial, you'll be able to write cleaner, DRY (Don't Repeat Yourself) SCSS code and understand how to effectively use the @extend directive.
Prerequisites:
- Basic understanding of CSS
- Familiarity with SASS/SCSS syntax
2. Step-by-Step Guide
Using @extend to Inherit Styles
In SASS, the @extend directive allows one selector to inherit the styles of another selector. This is extremely useful for reducing code duplication and keeping your stylesheets clean and organized.
Example:
.primary-btn {
background-color: blue;
color: white;
border-radius: 5px;
}
.secondary-btn {
@extend .primary-btn;
background-color: green;
}
In the example above, .secondary-btn will inherit all the styles from .primary-btn, but the background color will be overwritten to green.
Avoiding Code Duplication with @extend
The @extend directive can be a powerful tool for reducing code duplication. However, it's important to use this tool wisely. Overuse of @extend can lead to bloated CSS output and can make your code harder to maintain.
Limitations of @extend
While @extend can be a powerful tool, it's not always the best choice for every situation. It's important to understand its limitations:
@extendonly works with class and ID selectors. It doesn't work with pseudo-classes like:hoveror:focus.@extendcan lead to unintended consequences if used incorrectly. It can cause styles to be applied to elements unintentionally.
3. Code Examples
Example 1: Basic @extend
.panel {
border: 1px solid black;
padding: 10px;
}
.success-panel {
@extend .panel;
background-color: green;
}
In this example, .success-panel will inherit the border and padding properties from .panel and add the background-color property.
Example 2: Overriding Inherited Styles
.alert {
border: 1px solid red;
color: red;
}
.success-alert {
@extend .alert;
border-color: green;
color: green;
}
In this example, .success-alert will inherit the border and color properties from .alert but override the color values with green.
4. Summary
In this tutorial, we've learned how to use the @extend directive in SASS/SCSS to inherit styles from one selector to another, how to avoid code duplication, and the limitations of @extend.
For further learning, consider exploring other SASS features such as mixins, variables, and functions. Here are some additional resources:
- SASS Official Documentation
- SASS Basics Course on Codecademy
5. Practice Exercises
- Create a
.buttonclass with some basic styles and then create a.primary-buttonclass that extends.buttonand adds some additional styles. - Create a
.cardclass with a border and padding. Then create a.highlight-cardthat extends.cardand adds a background color. Finally, create a.error-cardthat extends.highlight-cardbut overrides the background color.
Solutions:
1.
.button {
padding: 10px 20px;
font-size: 16px;
}
.primary-button {
@extend .button;
background-color: blue;
color: white;
}
.card {
border: 1px solid black;
padding: 20px;
}
.highlight-card {
@extend .card;
background-color: yellow;
}
.error-card {
@extend .highlight-card;
background-color: red;
}
Remember to practice regularly to fully understand these concepts. Happy coding!
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