Sass @ extend and inheritance


Release date:2024-02-29 Update date:2024-02-29 Editor:admin View counts:165

Label:

Sass @ extend and inheritance

@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 .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

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;
}

Convert the above code to CSS code, as follows:

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;
}

Use @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.

Powered by TorCMS (https://github.com/bukun/TorCMS).