@extend
instruction tells Sass that the style of one selector is inherited from another selector.
If one style is almost the same as another, with only a few differences, usethe
@extend
seems very useful.
In the following Sass example, we created a basic button style Convert the above code to CSS code, as follows: Use
.button-basic
, then we define two button styles
.button-report
and
.button-submit
. They all inherited
.button-basic
main difference between them is the background color and font color, and all other styles are the same 13.7.1. Sass Code: ¶
.button-basic {
border: none;
padding: 15px 30px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button-report {
@extend .button-basic;
background-color: red;
}
.button-submit {
@extend .button-basic;
background-color: green;
color: white;
}
13.7.2. Css Code: ¶
.button-basic, .button-report, .button-submit {
border: none;
padding: 15px 30px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button-report {
background-color: red;
}
.button-submit {
background-color: green;
color: white;
}
@extend
after that, we do not need to specify multiple classes in the HTML button label
class="button-basic
button-report"
, you just need to set the
class="button-report"
, the class would be fine.
@extend
reflects the reuse of the code well.