css - sass 编译错误说 $color : null is not a color even though i declared color

标签 css sass

我是 sass 的新手,我写了一些 sass 代码,但它没有编译。

 $classes : primary secondary success warning danger;
    $colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);
    @each $class in $classes{
      .btn-#{$class}{
        $currentColor: map-get($colors,#{$class});
        background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
      }
    }

错误是:

$color: null is not a color.
stdin 14:55  root stylesheet on line 14 at column 55

但是当我用变量替换 linear-gradient 时它工作正常即

$classes : primary secondary success warning danger;
$colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);

    @each $class in $classes{
      .btn-#{$class}{
        $currentColor: map-get($colors,#{$class});
        background:$currentColor;
        //background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
      }
    }

这是编译成功的代码。

linear-gradient() 函数中 $currentColor 变量的作用和作用是什么

最佳答案

确实,将变量从 map-get 传递到其他 sass 函数确实有些问题。

但是你可以稍微修改你的代码,它会起作用:

$classes: primary secondary success warning danger;
$colors: (
    primary: ( normal: #007bff, light: lighten(#007bff,10%) ),
    secondary: ( normal: #6c757d, light: lighten(#6c757d,10%) ),
    success: ( normal: #28a745, light: lighten(#28a745,10%) ),
    warning: ( normal: #ffc107, light: lighten(#ffc107,10%) ),
    danger: ( normal: #dc3545, light: lighten(#dc3545,10%) )
);
@each $class in $classes{
  .btn-#{$class}{
    $currentColor: map-get(map-get($colors,#{$class}), normal);
    $currentColorLighten: map-get(map-get($colors,#{$class}), light);

    background: linear-gradient(to right, $currentColor, $currentColorLighten);
  }
}

你为每个类定义了两种颜色(普通版和亮版),然后通过双映射获取来使用它。

关于css - sass 编译错误说 $color : null is not a color even though i declared color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965297/

相关文章:

ruby-on-rails - 在 WIndows 10 和 ruby​​ 版本 2.5.0-1 上安装 SASS 时出错

sass - Sass 中具有可变参数列表的一个属性的多个值

javascript - 淡出 div 时保持文本

css - 截断两个跨度之间的跨度

javascript - 为什么开发人员要在 JavaScript 文件的样式表链接中附加查询字符串?

javascript - 获取/隐藏类中的第 N 个元素

ruby-on-rails - 学习如何结合 Blueprintcss 和 Compass/Sass 的最佳方式?

css - rails : Precompiled assets missing node modules

css - 如何在 Netbeans 中手动触发 LESS/SASS 编译器

javascript - 用于附加侧边栏的 Angular 指令