Table of contents
1.
Introduction
2.
What is @extend in SASS?
3.
Use Case of @extend in Sass
3.1.
Inheritance Chaining
3.2.
Multiple Inheritance
3.3.
Extending Selectors
3.4.
Placeholder Selector
4.
Frequently Asked Questions
5.
Key-Takeaways
Last Updated: Mar 27, 2024

Extend and Inheritance in Sass

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

For large-scale websites to look good, CSS stylesheets need to be organized, but as our projects grow, the stylesheets become more prominent, more complex, and harder to maintain. Thankfully, Sass can help simplify all of this. We can use Sass to access features not available in native CSS, like expressions, variables, nesting, mixins (Sass's way of referring to functions), inheritance, and more. Using the @extend keyword, we will demonstrate how inheritance is done in Sass.

What is @extend in SASS?

When using Sass, @extend is used for sharing CSS properties from one selector to another. It is one of the most important features of Sass.

Using Sass's @extend feature, we can share a set of properties between classes. Many classes put together in complicated CSS result in properties being duplicated. As a result of the @extend features, your code will be simpler to write and easier to rewrite.

Syntax:

@extend .class-name;

 

To demonstrate the @extend feature, let's take a look at an example. The following features are present in .Box and .Box1:

The .Box1 class is derived from the .Box class, and has the same properties as .Box.

style.scss

.Box {
    color: black;
    border: 1px solid black;
  }

.Box1 {
    @extend .Box;
    background-color: rgb(52, 40, 224);
}

 

By processing the above code, the .box CSS’s properties are applied to .Box1. This CSS facilitates avoiding writing multiple class names on HTML elements. 

The result is as follows:

style.css

.Box, .Box1 {
  color: black;
  border: 1px solid black;
}

.Box1 {
  background-color: #3428e0;
}

Use Case of @extend in Sass

Let’s learn some use-cases for @extend directive:

Inheritance Chaining

When constructing CSS, it is difficult to avoid duplicating properties between classes. Creating complex stylesheets can be a challenge. Chaining the @extend directive is also possible in object-oriented programming languages.

In the example given below, both .Box1 and .Box2 contains the properties of color and border with the same values.

style.scss

.Box {
    color: black;
    border: 1px solid black;
}

.Box1 {
    @extend .Box;
    background-color: rgb(52, 40, 224);
}

.Box2 {
    @extend .Box;
    background-color: rgb(78, 93, 105);
}

 

The result is as follows:

style.css

.Box, .Box1, .Box2 {
  color: black;
  border: 1px solid black;
}

.Box1 {
  background-color: #3428e0;
}

.Box2 {
  background-color: #4e5d69;
}

 

As seen in the rewritten CSS above, Sass simplifies our markup more than CSS alone.

Multiple Inheritance

It is common for one class to contain all the styles of another when designing a page. To address this, we typically use multiple classes in the HTML. A child class does not need to contain any properties of its own.

style.scss

.Box {
    color: black;
    border: 1px solid black;
}
  
.Box1 {
    background-color: rgb(52, 40, 224);
}

.Box2 {
    @extend .Box;
    @extend .Box1;
}

 

After Compilation, CSS will look like this:

style.css

.Box, .Box2 {
  color: black;
  border: 1px solid black;
}

.Box1, .Box2 {
  background-color: #3428e0;
}

Extending Selectors

You can extend more than just class selectors. It is also possible to simplify complex selectors into a single element, like Box:hover or .Box1 and so on. You can use the @extend directive to inherit styles from any selector, no matter how complex.

style.scss

.Box {
    color: black;
    border: 1px solid black;
}
  
.Box:hover {
    color: rgb(52, 40, 224);
}

.Box1 {
    @extend .Box:hover;
}

 

After Compilation, CSS will look like this:

style.css

.Box {
  color: black;
  border: 1px solid black;
}

.Box:hover, .Box1 {
  color: #3428e0;
}

Placeholder Selector

A % sign precedes the names of placeholder selectors. In HTML, placeholder selectors cannot be used, but they can be used as inheritance, meaning other selectors can inherit the styling by using the @extend at-rule.

style.scss

%Box {
    color: black;
    border: 1px solid black;
}

.Box1 {
    @extend %Box;
    color: rgb(52, 40, 224);
}

 

After Compilation, CSS will look like this:

style.css

.Box1 {
  color: black;
  border: 1px solid black;
}

.Box1 {
  color: #3428e0;
}

 

We do not have the placeholder selector in the compiled CSS, but only its styling rules in the styling classes that extend it.

Frequently Asked Questions

  1. What are the advantages of @extend?
    Ans: Cleaner HTML Classes: In cases where you run into issues, you may not determine the root cause if so many classes are used within one element. Furthermore, HTML tags are not organized well. Our classes are structured in a cleaner way using @extend within each HTML element.
    Reducing Duplication of CSS: The CSS styles we use in web development are often duplicated. The reuse of CSS source code is, therefore, beneficial. We can use @extend to reuse our CSS wherever it makes sense and is cleaner to do so.
    Saving time and effort: By reusing CSS for different elements, developers can reduce the amount of time spent finding the cause of CSS issues and make their CSS structure cleaner.
     
  2. What is SASS Private Placeholder?
    Ans: Private placeholders can be defined in SASS by adding a - hyphen or underscore _ before the selector name. Placeholder selectors become private and can only be altered by styling classes within the same stylesheet.
     
  3. When should we use @mixin and @extend?
    Ans: Many of the capabilities of the @extend directive are the same as Sass mixins.
    Mixins copy their rules into the styles that include them. The @extend directive allows selectors to be only included once for any given rule.
    In contrast, mixins are smaller when compressed with gzip. When the style rules need to be configured using arguments, we should use SASS mixins. However, if the style rules are static (just a chunk of style), we can use SASS extends in our stylesheet.

Key-Takeaways

Using Sass, we can improve our CSS in many ways to make it easier to develop, maintain, and be more readable. Property is not copied into an inheriting selector; instead, it is combined with it. By using the @extend rule, we inherit from a property named after the property we want to inherit from.

If you are interested in learning front-end web development, check out Coding Ninjas Guided Path which contains all the content for it. 

Live masterclass