You are reading the article How @Extend Directive Works In Sass With Examples updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How @Extend Directive Works In Sass With Examples
Introduction to SASS @extendCoordinating CSS style sheets is becoming essential to effectively styling large-scale websites and are getting bigger, more complicated, and harder to manage as they develop. The SASS provides an effective @extend directive that includes the set of CSS properties that is transferred from one selector to another using this directive. That would be to say it requires CSS properties to be inherited from another selector. The @extend directive in SASS is an effective directive which makes it easier for selectors to share rules and relationships. In SASS, the @extend directive is a way to encompass and re-use styles. The @extend directive is helpful when you have elements that are almost similarly styled and vary only in some small things.
Syntax:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax for SASS @extend directive can be written as shown below:
.demo_one { color: cyan; border: 1px dotted black; } .demo_two { @extend .demo_one; background-color: red; }In the above SCSS code, .demo_two class inherit all the CSS properties from the .demo_oneclass through @extend directive.
.demo_one, .demo_two{ color: cyan; border: 1px dotted black; } .demo_two { background-color: red; }The .demo_twoinherits from .demo_one which contains all the properties of .demo_oneparent class.
How @extend Directive works in SASS?
The @extend directive will work very effectively to reuse CSS for different elements, minimizing the effort required to find the root cause of CSS difficulties and to keep their CSS structure simple and clean.
The @extend directive works by updating the style rules that include the extended selector and @extend directive is one of the best choices when we express a connection between semantic classes.
Examples of SASS @extendGiven below are the examples mentioned:
Example #1Let’s create an example to make use of the @extend directive in SASS. Here, we have created an HTML file called chúng tôi with the below code:
Code:
example 1.html
Now create a file called sass_extend1.scss with the below code:
Code:
sass_extend1.scss
body { font-family: Arial, Helvetica, sans-serif; } .main_class { color: #333; border: 1px solid #FF6347; box-shadow: 3px 5px 0 #00ff99; margin: 0 0 10px; padding: 12px; } .class1 { @extend .main_class; background-color: #ccffcc; } .class2 { @extend .main_class; background-color: #f2e6ff; } .class3 { @extend .main_class; background-color: #ffccf2; }Now open the command prompt and run the below command to watch the file and communicates it to SASS and updates the CSS file every time SASS file changes.
Code:
sass –watch sass_extend1.scss: sass_extend1.cssNow, execute the file with the above command and it will create the sass_extend1.css file with the below code:
Code:
sass_extend1.css
body { font-family: Arial, Helvetica, sans-serif; } .main_class, .class3, .class2, .class1 { color: #333; border: 1px solid #FF6347; box-shadow: 3px 5px 0 #00ff99; margin: 0 0 10px; padding: 12px; } .class1 { background-color: #ccffcc; } .class2 { background-color: #f2e6ff; } .class3 { background-color: #ffccf2; }Output:
Save the above given html code in html file.
Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.
Example #2Now, create an HTML file called chúng tôi with the below code:
Code:
example2.html
EDUCBA : It is a leading global provider of skill based education. It is an online learning model along with amazing 2500+ courses prepared by top-notch professionals.
Now create a file called sass_extend2.scss with the below code:
Code:
sass_extend2.scss
{ font-family: Arial, Helvetica, sans-serif; } .main_class { background-color: #99e6ff; border: 2px dotted green; box-shadow: 5px 5px 0 #cc6600; padding: 15px; } .class2 { @extend .main_class; background-color: #ffffb3; color: #ff80ff; }Execute the file with the above command as shown in the previous example and it will create the sass_extend2.css file with the below code.
Code:
sass_extend2.css
body { font-family: Arial, Helvetica, sans-serif; } .main_class, .class2 { background-color: #99e6ff; border: 2px dotted green; box-shadow: 5px 5px 0 #cc6600; padding: 15px; } .class2 { background-color: #ffffb3; color: #ff80ff; }Output:
Save the above given html code in html file.
Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.
Example #3You could also integrate complex selectors into one single element, like a: hover and many more.
Create an HTML file called chúng tôi with the below code:
Code:
example3.html
Now create a file called sass_extend3.css with the below code:
Code:
sass_extend3.css
body { font-family: Arial, Helvetica, sans-serif; } a { text-decoration: underline; } .info, .class_link { color: #ff3385; } .info:hover, .class_link { color: #4747d1; font-size: 20px; }Output:
Save the above given html code in html file.
Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.
ConclusionIn this article, we have seen how the @extend directive can be used to organize our CSS classes within each HTML element in a simpler way. This directive is very efficient on the pre-processor level and will be even greater on the browser layer. The reuse of written CSS code in web creation may be immensely important. The @extend directive will enable us to reuse the CSS when required and to use it more cleanly.
Recommended Articles
This is a guide to SASS @extend. Here we discuss the introduction to SASS @extend, how do the directive works with respective examples for better understanding. You may also have a look at the following articles to learn more –
You're reading How @Extend Directive Works In Sass With Examples
Update the detailed information about How @Extend Directive Works In Sass With Examples on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!