SASS/SCSS / Inheritance and Extends

Understanding the Limitations of @extend

This tutorial will help you understand the limitations of using @extend in SASS/SCSS. By knowing these limitations, you can use @extend more effectively and avoid potential issues.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers how to reuse styles with inheritance and @extend in SASS/SCSS.

Understanding the Limitations of @extend

1. Introduction

The goal of this tutorial is to help you understand the limitations of using the @extend directive in SASS/SCSS. You will learn about the potential issues that can arise when using @extend and how to use it more effectively.

Prerequisites:
Basic knowledge of SASS/SCSS and CSS is required.

2. Step-by-Step Guide

The @extend directive in SASS/SCSS allows one selector to inherit the styles of another selector. It's a powerful feature, but it has its limitations and potential issues.

The primary limitation is that @extend can only extend simple selectors. This means it cannot extend complex selectors or selectors with pseudo-classes.

Another limitation is that using @extend can lead to bloated CSS output if not used carefully. This is because every time you use @extend, SASS/SCSS generates all the possible combinations of selectors.

Best Practices and Tips:
1. Use @extend sparingly to avoid bloated CSS output.
2. Instead of @extend, consider using mixins or placeholder selectors where appropriate.
3. Always review your compiled CSS to ensure it is as efficient as possible.

3. Code Examples

Example 1: Simple @extend

.error {
  border: 1px solid red;
}

.warning {
  @extend .error;
  background-color: yellow;
}

In the above example, .warning extends .error, meaning it inherits the border style from .error. The compiled CSS will be:

.error, .warning {
  border: 1px solid red;
}

.warning {
  background-color: yellow;
}

Example 2: Limitation of @extend

.error:hover {
  border: 1px solid red;
}

.warning {
  @extend .error:hover;
  background-color: yellow;
}

In the second example, we try to @extend a pseudo-class, which is not allowed. This will throw an error in SASS/SCSS.

4. Summary

In this tutorial, you've learned about the limitations of @extend in SASS/SCSS. You've also seen how to use @extend correctly and some best practices to follow.

Next, consider exploring other features of SASS/SCSS such as mixins and placeholder selectors.

Additional Resources:
SASS/SCSS Documentation

5. Practice Exercises

Exercise 1:
Write SCSS code to style a .success class, that extends the styles of a .message class.

Solution:

.message {
  border: 1px solid green;
  color: green;
}

.success {
  @extend .message;
  background-color: lightgreen;
}

Exercise 2:
Given the following SCSS, what will be the compiled CSS output?

.btn {
  padding: 10px;
  border-radius: 5px;
}

.btn-primary {
  @extend .btn;
  background-color: blue;
  color: white;
}

Solution:

.btn, .btn-primary {
  padding: 10px;
  border-radius: 5px;
}

.btn-primary {
  background-color: blue;
  color: white;
}

Keep practicing with different scenarios to get more comfortable with @extend and its limitations.

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

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

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