如何创建Sass mixin别名并支持内容块?

时间:2018-04-26 08:44:28

标签: css sass scss-mixins

如何为同一个mixin定义多个名称,并支持内容块?

定义

@mixin desktop-breakpoint {
   @media only screen and (min-width: 769px) {
      @content;
   }
}

@mixin large-breakpoint {
   @include desktop-breakpoint;
}

用法

.my-class {
   font-size: small;

   @include desktop-breakpoint {
      font-size: big;
   }
}

.my-other-class {
   color: red;

   @include large-breakpoint {
      color: blue;
   }
}

错误消息

  

Mixin"大断点"不接受内容块。

1 个答案:

答案 0 :(得分:4)

@content mixin中使用@include desktop-breakpoint时,您没有传递任何large-breakpoint。这样做会修复编译错误:

@mixin large-breakpoint {
   // Remember to pass content received by mixin to @include
   @include desktop-breakpoint {
     @content;
   }
}

然后您的CSS将按预期正确编译:

.my-class {
  font-size: small;
}
@media only screen and (min-width: 769px) {
  .my-class {
    font-size: big;
  }
}

.my-other-class {
  color: red;
}
@media only screen and (min-width: 769px) {
  .my-other-class {
    color: blue;
  }
}

根据修改过的代码查看概念验证示例:https://www.sassmeister.com/gist/3109af060293eed0b89a22c27fa20527