Yogesh Chauhan's Blog

How does @extend rule work in SCSS (Sass)?

in SCSS on May 3, 2021

@extend rule is helpful in inheriting styles from another selector.

Just like this:

SassScript is pretty handy when it comes to inherit the styles from one selector to another. If the same style applied to another element or selector even without a single rule change, you can just extend the rule. You’d want to do that in a case where you want to manage both selectors style parallely and one of them might get a slight style change in future. There are multiple ways to do that but this involves less coding.

As you can see in the example above, all we need to do is @extend [selector’s name].

This is how it compiles into CSS:


.error, .error--serious {
  color: red;
}
.error--serious {
  color: red;
  font-weight: bold;
  font-size: 20px;
}

The elements are styled EXACTLY like they matched the extended selector.

That means if you extend an hover selectors style, it will be used for the hovered state style for that element too. Just like this:

Extend rules are resolved after the stylesheet is compiled completely.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
UNION and UNION ALL in PostgresPostgresSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscellaneousLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPHow to send and receive query strings via links in Angular 9?AngularHow to create two segues with two UIButtons on a single page (Swift 5.0)?SwiftSorting Arrays in JavaScriptJavaScriptHow SCSS (Sass) finds a module without a file extension?SCSSCreate a responsive image gallery using CSS gridCSSWhat is Zdog?MiscellaneousComponents and Props in ReactReactHow to Install PHP composer in Mac OS Catalina?PHPHow to display and animate image on scroll using JavaScript?JavaScript