首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sass中的本机css混频器

sass中的本机css混频器
EN

Stack Overflow用户
提问于 2017-03-07 16:57:06
回答 1查看 720关注 0票数 0

我尝试使用本机css混合器来构建另一个css混合器,但是在编译时会出现以下错误:

代码语言:javascript
复制
$blue:            #29579b;

:root {
  --edi-blue: $blue;

  --text: {
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: var(--edi-blue);
  }

  --text--bold: {
    @apply --text;
    font-weight: bold;
  }
}

.someclass {
  @apply --text--bold;
}

错误:

代码语言:javascript
复制
[16:48:58] Starting 'sass'...
[16:48:58] Finished 'sass' after 7.22 ms
Error in plugin 'sass'
Message:
    src/shared/styles/sass/webcomponents-shared-styles.scss
Error: Illegal nesting: Only properties may be nested beneath properties.
        on line 11 of src/shared/styles/sass/webcomponents-shared-styles.scss
>>     @apply --text;
   ----^

编辑:

对于@apply规则,如果您在全局范围(:root)上定义了@apply规则,它总是只使用来自该作用域的变量,因此不能传递本地值。

所以我试着用sass混音器做同样的事情:

代码语言:javascript
复制
@mixin --text() {
  font-style: normal;
  font-stretch: normal;
  text-align: center;
  color: var(--edi-blue);
}

:root {
  --edi-blue: $blue;

  --text: {
    @include --text;
  }

  --text--bold: {
    @include --text;
    font-weight: bold;
  }
}

.someclass {
  @apply --text--bold;
}

问题是代码生成:

代码语言:javascript
复制
:root {
  --text-font-style: normal;
  --text-font-stretch: normal;
  --text-text-align: center;
  --text-color: var(--edi-blue);
  --text--bold-font-style: normal;
  --text--bold-font-stretch: normal;
  --text--bold-text-align: center;
  --text--bold-color: var(--edi-blue);
  --text--bold-font-weight: bold; }

.someclass {
  @apply --text--bold; }

而不是:

代码语言:javascript
复制
:root {
  --text: {
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: var(--edi-blue);
  }

  --text--bold: {
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: var(--edi-blue);
    font-weight: bold;
  }
}

    .someclass {
      @apply --text--bold; }
EN

回答 1

Stack Overflow用户

发布于 2017-03-07 17:14:01

不幸的是,它不能用于@apply规则,就好像您在全局范围(:root)上定义它一样--它总是只使用来自该作用域的变量,因此不能传递您的本地值。

这是可悲的,但至少您可以使用预处理器和本地CSS变量将您的混合文件复制/粘贴到您的“作用域”。

应该是这样的:

代码语言:javascript
复制
$blue:            #29579b;

:root {
  --edi-blue: $blue;

  --text: {
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: var(--edi-blue);
  }
}

.text--bold {
   @apply --text;
   font-weight: bold;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42653946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档