Sass @extend and Inheritance

Sass @extend instructie

The @extend directive allows you to specify multiple classes for elements in HTML code without having to specify them individually, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get both styles. instructie staat je toe om een groep CSS-eigenschappen van een selector te delen met een andere selector.

Als je bijna identieke elementen hebt, maar die slechts op enkele kleine details verschillen, dan The @extend directive allows you to specify multiple classes for elements in HTML code without having to specify them individually, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get both styles. instructie zeer nuttig.

De volgende Sass voorbeeld maakt eerst een basisstijl voor de knop aan (deze stijl wordt gebruikt voor de meeste knoppen). Vervolgens maken we een stijl voor de "Rapport" knop en een stijl voor de "Indienen" knop. De "Rapport" en "Indienen" knoppen worden allemaal via The @extend directive allows you to specify multiple classes for elements in HTML code without having to specify them individually, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get both styles. Instructies erfgen alle CSS-eigenschappen van de .button-basic klasse. Bovendien definiëren ze hun eigen kleuren:

SCSS syntaxis:

.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 look like this:

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 having to specify them individually, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get both styles. By using

The @extend directive allows you to specify multiple classes for elements in HTML code without having to specify them individually, for example: <button class="button-basic button-report">Report this</button>. You only need to specify .button-report to get both styles. The directive helps keep your Sass code very dry (DRY).