Sass @extend and Inheritance

Sass @extend 指令

The @extend directive, you do not need to specify multiple classes for elements in HTML code, for example: <button class="button-basic button-report">Report this</button>. You just need to specify .button-report to get two sets of styles. 指令允许您将一组 CSS 属性从一个选择器共享到另一个选择器。

如果您有几乎相同样式的元素,但仅在一些小细节上有所不同,则 The @extend directive, you do not need to specify multiple classes for elements in HTML code, for example: <button class="button-basic button-report">Report this</button>. You just need to specify .button-report to get two sets of styles. 指令很有用。

下面的 Sass 实例首先为按钮创建一个基本样式(此样式将用于大多数按钮)。然后,我们为 "Report" 按钮创建一种样式,为 "Submit" 按钮创建一种样式。"Report" 和 "Submit" 按钮都通过 The @extend directive, you do not need to specify multiple classes for elements in HTML code, for example: <button class="button-basic button-report">Report this</button>. You just need to specify .button-report to get two sets of styles. 指令继承了 .button-basic 类的所有 CSS 属性。此外,它们还定义了自己的颜色:

SCSS 语法:

.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, you do not need to specify multiple classes for elements in HTML code, for example: <button class="button-basic button-report">Report this</button>. You just need to specify .button-report to get two sets of styles. By using

The @extend directive, you do not need to specify multiple classes for elements in HTML code, for example: <button class="button-basic button-report">Report this</button>. You just need to specify .button-report to get two sets of styles. The directive helps keep your Sass code very dry (DRY).