【问题标题】:Why is my sass mixin not compiling into css?为什么我的 sass mixin 没有编译成 css?
【发布时间】:2019-02-01 16:58:56
【问题描述】:

我是 sass 新手,目前正在探索它。我想尝试使用 mixin,但是当我尝试它时,它就是行不通。代码如下:

@mixin container($radius, $width, $height, $bg, $color) {
    -webkit-border-radius: $radius;
    -moz-border-radius: $radius;
    -ms-border-radius: $radius;
    border-radius: $radius;

    $width: $width;
    $height: $height;

    $bg: $bg;
    $color: $color;

    margin: 0 auto;
}

.container {
    @include container(10px, 80%, 700px, $white, $dark-grey)
}

所以我有一个 div 和 class 容器,想把 mixin 放在那里,但它就是不工作。有人可以帮忙吗??

【问题讨论】:

    标签: sass scss-mixins


    【解决方案1】:

    这个?

    $white: #fff;
    $dark-grey: #666;
    
    @mixin container($radius, $width, $height, $bg, $color) {
        -webkit-border-radius: $radius;
        -moz-border-radius: $radius;
        -ms-border-radius: $radius;
        border-radius: $radius;
    
        width: $width;
        height: $height;
    
        background: $bg;
        color: $color;
    
        margin: 0 auto;
    }
    
    .container {
        @include container(10px, 80%, 700px, $white, $dark-grey)
    }
    

    【讨论】:

    • 这行得通!我才意识到我的问题在哪里。非常感谢您的帮助!
    【解决方案2】:

    看起来你的变量有点混乱

    这应该有效:

    @mixin container($radius, $width, $height, $bg, $color) {
        -webkit-border-radius: $radius;
        -moz-border-radius: $radius;
        -ms-border-radius: $radius;
        border-radius: $radius;
    
        width: $width;         //  <= here
        height: $height;       //  <= here
    
        background-color: $bg; //  <= here
        color: $color;
    
        margin: 0 auto;
    }
    
    .container {
        @include container(
          10px, 
          80%, 
          700px, 
          white,    // <= here
          darkgrey  // <= here
        )
    
    }
    
    

    CSS

    
    .container {
      border-radius: 10px;
      width: 80%;
      height: 700px;
      background-color: white;
      color: darkgrey;
      margin: 0 auto;
    }
    
    

    【讨论】:

    • 不,遗憾的是它仍然无法正常工作,顺便说一下颜色来自我之前声明的变量:(
    • 那么还有其他问题——我在下面添加了输出。我只是替换了 $white 和 $dark-grey 因为它们没有在您的示例中显示 - 如果它们只是离开它们:)。链接:sassmeister.com/gist/2eced6d6a821713f0bc4e28584962ff3
    猜你喜欢
    • 2018-09-30
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2019-04-29
    • 2015-07-30
    • 2023-03-31
    相关资源
    最近更新 更多