简体   繁体   English

SASS中@ extend-only选择器(占位符)的范围

[英]Scope of the @extend-only selectors (placeholders) in SASS

I thought the following SCSS would end in error when compiling: 我认为以下SCSS在编译时会以错误结束:

.main {

  %abstract {
    color: red;
  }

  .main-element {
    @extend %abstract;
  }

}

.outside {
  @extend %abstract; // <-- 
}

While it actually compiles to: 虽然它实际编译为:

.main .main-element, .main .outside {
  color: red;
}

Is there any way to make such placeholders not available ouside the scope , ie only available for children of .main ? 是否有任何方法可以使这些占位符不在范围内 ,即仅适用于.main.main

See http://sass-lang.com/documentation/file.SASS_REFERENCE.html#placeholders 请参阅http://sass-lang.com/documentation/file.SASS_REFERENCE.html#placeholders

You don't. 你没有。 Nesting only provides scope to variables (as of Sass 3.3) or mixins, not selectors. 嵌套仅为变量(从Sass 3.3开始)或mixins而非选择器提供范围。 A placeholder class is the same as any other class, except for the part where its name is not output in the compiled CSS. 占位符类与任何其他类相同,除了在编译的CSS中未输出其名称的部分。

If this behavior is required, then you're stuck using mixins (which can, in turn extend the desired selector) 如果需要这种行为,那么你就会使用mixins(它可以反过来扩展所需的选择器)

%foo {
  color: red;
}

.main {

  @mixin foo() {
    @extend %foo;
  }

  .main-element {
    @include foo();
  }

}

.outside {
  @include foo(); // <-- yep, it errors here
}

You could also use @at-root 你也可以使用@ at-root

%abstract {
  color: red;
}

.main {

  .main-element {
    @extend %abstract;

    @at-root .outside {
      @extend %abstract;
    }
  }
}

output 产量

.main .main-element, .outside {
  color: red;
}

or just move the placeholder outside specific main block 或者只是将占位符移动到特定主块之外

%abstract {
    color: red;
  }

.main {

  .main-element {
    @extend %abstract;
  }
}

.outside {
  @extend %abstract;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM