Best Practices for Inheritance in SASS/SCSS

Tutorial 5 of 5

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:

  • @extend only works with class and ID selectors. It doesn't work with pseudo-classes like :hover or :focus.
  • @extend can 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

  1. Create a .button class with some basic styles and then create a .primary-button class that extends .button and adds some additional styles.
  2. Create a .card class with a border and padding. Then create a .highlight-card that extends .card and adds a background color. Finally, create a .error-card that extends .highlight-card but 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!