【问题标题】:SCSS Variables as @extend classSCSS 变量作为@extend 类
【发布时间】:2013-07-09 20:19:29
【问题描述】:

我的想法是我想为input[type=text]input[type="password"]input[type=submit] 编写静默类。然后我会将它们作为变量传递给 mixin 中的 @extend 它们。

我的解析器抛出这个错误;

Syntax error: Invalid CSS after "   @extend ": expected selector_sequence, was "$type;"

这是我的代码;

%text {
    (text styling)
}

%password {
    @extend %text;
}

%submit {
    padding: .5em;
    background-color: $button-color;
    border: none;
    cursor: pointer;
    color: white;
    border: 1px solid darken($button-color, 20%);
    &:hover {
        @include transition;
        background-color: darken($button-color, 10%);
    }
}

@mixin input($type) {
    margin-bottom: 1.5em;
    margin-left: 0;
    outline: none;
    @extend $type;
}

任何帮助将不胜感激

【问题讨论】:

    标签: css sass


    【解决方案1】:

    尝试使用变量插值

    @extend #{$type};
    

    更多信息SASS Reference

    【讨论】:

    • 它不再为此抛出错误,但现在是我尝试扩展的时候。我正在使用@extend input(%text); 行,出现的错误是Invalid CSS after "@extend input": expected "}", was "(%text);"
    • 你不能扩展 mixin,只能扩展一个简单的选择器(类、id、元素等)。此外,在将选择器作为参数传递给 mixin 时,您需要引用它(例如,@include input('%foo'))。
    【解决方案2】:

    虽然 Fabrizio 的回答在形式上是正确的,但请考虑不要那样做。

    任何类型的编程都有一条伟大的规则:“保持简单,愚蠢!”又名KISS

    虽然 SASS 提供了扩展和混合等高级工具,但这并不意味着您应该尽可能多地使用它们。不必要的时候不要让你的代码变得复杂!

    此代码完全符合您的要求:将样式应用于input[...] 选择器:

    input {
        margin-bottom: 1.5em;
        margin-left: 0;
        outline: none;
    }
    
    input[type=text], input[type=password] {
        font-family: Verdana; // Text styles
    } 
    
    input[type=submit]  {
        padding: .5em;
        background-color: $button-color;
        border: none;
        cursor: pointer;
        color: white;
        border: 1px solid darken($button-color, 20%);
        &:hover {
            @include transition;
            background-color: darken($button-color, 10%);
        }
    }
    

    如果您想将样式应用于自定义类/ID,请考虑以下方法:

    /////////////////
    // Silent classes
    /////////////////
    
    %input {
        margin-bottom: 1.5em;
        margin-left: 0;
        outline: none;
    }
    
    %text {
        @extend %input;
        font-family: Verdana;
    }
    
    %password {
        @extend %text;
    }
    
    %submit {
        @extend %input;
        padding: .5em;
        background-color: $button-color;
        border: none;
        cursor: pointer;
        color: white;
        border: 1px solid darken($button-color, 20%);
        &:hover {
            @include transition;
            background-color: darken($button-color, 10%);
        }
    }
    
    
    
    ///////////////////////////
    // Applying silent classes:
    ///////////////////////////
    
    .some .weirdly .nested input[type=text] {
        @extend %text;
    }
    
    .password {
        @extend %password;
    }
    
    #the-submit-button {
        @extend %submit;
    }
    

    演示:http://sassbin.com/gist/5956909/

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2013-11-02
      • 2020-08-08
      • 2023-04-03
      • 2021-05-13
      • 2019-03-31
      • 2022-10-05
      • 2012-06-27
      • 2020-01-24
      相关资源
      最近更新 更多