Sass @extend and Inheritance
- Previous Page Sass @mixin
- Next Page Sass Strings
Sass @extend directive
The @extend directive allows you to specify multiple classes for elements in HTML code without repeating them, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get two sets of styles.
directive allows you to share a set of CSS properties from one selector to another.
If you have elements with almost the same style but only differ in some minor details, then The @extend directive allows you to specify multiple classes for elements in HTML code without repeating them, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get two sets of styles.
directive is very useful.
The following Sass example first creates a basic style for the button (this style will be used for most buttons). Then, we create a style for the "Report" button and a style for the "Submit" button. Both the "Report" and "Submit" buttons are through The @extend directive allows you to specify multiple classes for elements in HTML code without repeating them, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get two sets of styles.
The directive inherits all CSS properties of the .button-basic class. In addition, they define their own colors:
SCSS syntax:
.button-basic { .button-basic, .button-report, .button-submit { border: none; padding: 15px 30px; text-align: center; font-size: 16px; color: white; cursor: pointer; @extend .button-basic; .button-report { color: white; background-color: red; @extend .button-basic; .button-submit { background-color: green; color: white;
编译后,CSS 将如下所示:
After compilation, the CSS will be as follows:
CSS Output: .button-basic, .button-report, .button-submit { border: none; padding: 15px 30px; text-align: center; font-size: 16px; color: white; cursor: pointer; .button-report { color: white; background-color: red; .button-submit { background-color: green; color: white;
} The @extend directive allows you to specify multiple classes for elements in HTML code without repeating them, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get two sets of styles.
By using
The @extend directive allows you to specify multiple classes for elements in HTML code without repeating them, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get two sets of styles.
The directive helps keep your Sass code very dry (DRY).
- Previous Page Sass @mixin
- Next Page Sass Strings