将 `!important` 添加到 mixin

saw*_*awa 5 css sass

我想添加!important到 mixin。我尝试了以下两种方法,它们都返回错误:

@include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)); !important

@include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)) !important;
Run Code Online (Sandbox Code Playgroud)

有没有办法正确地做到这一点?

Reu*_*iro 8

在某些情况下,我使用一个名为 的可选参数$important,我可以传入它true

例子:

my-mixin($important: true);
Run Code Online (Sandbox Code Playgroud)

它看起来像这样,有一个辅助函数来避免重复我必须切换的属性important

@function if-important($important){
  @return #{if($important, '!important', '')};
}

@mixin my-mixin($important: false) {
  border-radius: 0; //whatever
  border: 1px solid #ccc if-important($important);
  background-color: #fff if-important($important);
  color: #000 if-important($important);
}
Run Code Online (Sandbox Code Playgroud)


Tik*_*kes 1

你不能在 Mixin 上使用 !important。

它最终会给你一个 SASS 语法错误。

请参阅此问题以获取更多信息