Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
Sass @extend Rule
Next article icon

Sass @extend Rule

Last Updated : 18 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The sass @extend rule can be used when it is necessary for a class to have the styles of another class, along with its own specific styles. This rule tells Sass that one selector should inherit the class from another selector.

Syntax:

@extend <selector>

Example: This example shows the use of the SASS @extend rule.

SASS
.gfg       border: 1px green       background-color: black       font-family: sans-serif  &--geeks     @extend .gfg     border-width: 4px 

Output:

.gfg,
.gfg--geeks {
border: 1px green;
background-color: black;
font-family: sans-serif;
}

.gfg--geeks {
border-width: 4px;
}

Sass styles are the elements that match the extender as though they also match the class being extended when one class extends the other. Selectors are not just used on their own in style rules. Sass knows to extend everywhere the selector is used. This ensures that the elements are styled exactly as if they matched the extended selector.

Example: This example shows the use of the SASS @extend rule. 

SASS
.gfg:hover      border: 1px green       background-color: black       font-family: sans-serif  .gfg--geeks       @extend .gfg       border-width: 4px 

Output: 

.gfg:hover,
.gfg--geeks:hover {
border: 1px green;
background-color: black;
font-family: sans-serif;
}

.gfg--geeks {
border-width: 4px;
}

Working of the rule: The @extend rule updates the style rules that contain the extended selector so that it contains the extending selector. When extending selectors, Sass does intelligent unification by never generating the selectors like #main#footer that cannot possibly match any elements. It also ensures that complex selectors are interleaved so that they work no matter which orders the HTML elements are nested in.

Intelligent unification is also done by trimming the redundant selectors as much as possible, while still ensuring that the specificity is greater than or equal to that of the extender. In short, it intelligently handles combinators, universal selectors, and pseudo-classes that contain selectors.

Example: This example shows the use of the SASS @extend rule.

css
.content nav.sidebar       @extend .gfg   /* This won't be extended, because    `p` is incompatible with `nav` */ p.gfg       background-color: green       color: white   /* There is no way to know whether   `<div class="guide">` will be inside    or outside `<div class="content">`,    so Sass generates both to be safe */ .geeks .gfg       border: 1px solid black       border-radius: 4px   /* Sass knows that every element     matching "main.content" also     matches ".content" and avoids    generating unnecessary     interleaved selectors */ main.content .gfg       font-size: 2px       font-family: sans-serif 

Output:

p.gfg {
background-color: green;
color: white;
}

.geeks .gfg,
.geeks .content nav.sidebar,
.content .geeks nav.sidebar {
border: 1px solid black;
border-radius: 4px;
}

main.content .gfg,
main.content nav.sidebar {
font-size: 2px;
font-family: sans-serif;
}

Sass's intelligent unification can be directly accessed using the selector functions. The selector.unify() function returns a selector that matches the intersection of two selectors, while the selector.extend() function works just like @extend, but on a single selector.


Next Article
Sass @extend Rule

S

Slash_IT
Improve
Article Tags :
  • Web Technologies
  • HTML
  • CSS
  • SASS

Similar Reads

    Sass | At-rules
    At-rules are simply some CSS statements that instructs the CSS how to behave in particular conditions. They start with an "@" sign, followed by an identifier and ends with a semicolon (in case of SCSS), or the next CSS statement (in case of SASS), depending upon the Sass syntax used.  Syntax: css @i
    2 min read
    Sass @each Rule
    The @each rule is used to emit styles or evaluate codes for each element of a list or each pair of a map. It is most advantageous for repeating styles that have a few differences in them. The section is evaluated for every element of the list or pair of the map which has been assigned to the provide
    2 min read
    Less.js Extend
    LESS.js is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables and, so on, which help to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will see the basic usage
    3 min read
    Sass @function Rule
    Functions can define complex operations on SassScript values that can be reused throughout the stylesheet. It makes it easier to abstract out the common formulas and behaviors in some readable way. Functions can be defined using @function at-rule. Syntax: html @function <function name>(<arg
    2 min read
    SASS @for Rule
    The @for rule in SASS is used to count up or down from one number to another and check the section for every number that comes in between the given range. Every number is assigned a variable name that can be accessed in the loop. It can be used in two ways: 1. start through end: In this type, the "s
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences