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…

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Covers 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:

  • @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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help