![]() |
In this article, you will see how to use Selector Inheritance and Multiple Inheritance using SASS. In SASS(Syntactically Awesome Style Sheets), a CSS preprocessor, we can create selector inheritance and multiple inheritances through the use of mixins and extends. The problem with using inheritance with SASS is that here you have to use other Selector Inheritance (Using SASS)Selector inheritance in CSS describes how styles cascade from parent elements to child elements. Unless overridden by particular styles applied explicitly to the child components, a style applied to a parent element may be inherited by its descendant elements. This enables styles to spread throughout the DOM tree. SASS allows you to inherit attributes from other selectors, it also helps you write less code and/or combine fewer elements. To implement the selector inheritance using SASS you have to carry on the styles of one selector into some other selector’s set of CSS rules by using the @extend rule like in the syntax below. Syntax:.parent { Multiple Inheritance (Using SASS)Multiple inheritances in CSS refer to the capacity to apply styles to an element from various sources (classes or selectors). Although multiple inheritances are not natively supported by CSS, you can nevertheless get a comparable result by mixing classes or using multiple selectors. In CSS, using several classes on an element or applying styles to elements using different selectors can produce multiple inheritance-like effects. With Sass, you can quickly reuse and mix styles from several selectors and accomplish multiple inheritances. To implement the multiple inheritance using SASS you have to carry on the styles of several selectors into another selector’s set of CSS rules by using the @extend rule multiple times inside the selector style rules extending the different selectors like in the syntax below. Syntax:.class1 { Example 1: The code example demonstrates how we can implement the selector inheritance in SASS using the above-given syntax. Here the first <p> element inherits from the parent div and it stays different from the other <p> element using the @extend at-rule. index.html:
styles.sass: This sass file contains the main stylesheet and is compiled into .css
The above sass code is compiled into CSS using SASS preprocessor in the command line like this: sass styles.sass style.css style.css: The compiled CSS code:
Output: Example 2: The code example demonstrates the implementation of Multiple Inheritance using the above approach and syntax. Here the first container takes one .class3 class which inherits both the .class1 and .class2:
styles.sass: This sass file contains the main stylesheet and is compiled into .css
The above sass code is compiled into CSS using SASS preprocessor in the command line like this: sass styles.sass style.css style.css: The compiled CSS code:
Output: |
Reffered: https://www.geeksforgeeks.org
CSS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |