简体   繁体   English

SCSS 变量作为@extend 类

[英]SCSS Variables as @extend class

My idea is that I would like to write silent classes for input[type=text] , input[type="password"] and input[type=submit] .我的想法是我想为input[type=text]input[type="password"]input[type=submit]编写静默类。 I would then @extend them in a mixin by passing hem through as a variable.然后我会通过将下摆作为变量传递来@extend它们在混合中。

My parser is throwing this error;我的解析器抛出这个错误;

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

Here is my code;这是我的代码;

%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;
}

Any help would be appreciated任何帮助,将不胜感激

try using variables interpolation尝试使用变量插值

@extend #{$type};

Further information on SASS Reference有关SASS 参考的更多信息

While Fabrizio's answer is formally correct, consider not going that way.虽然 Fabrizio 的回答在形式上是正确的,但请考虑不要那样做。

There's a great rule in programming of any kind: "keep it simple, stupid!"任何类型的编程都有一个很好的规则:“保持简单,愚蠢!” aka KISS .又名亲吻

Though SASS provides such advanced facilities as extends and mixins, it doesn't mean that you should use them as much as possible.尽管 SASS 提供了诸如扩展和混合之类的高级工具,但这并不意味着您应该尽可能多地使用它们。 Don't make your code complicated when you don't have to!不需要时不要让代码变得复杂!

This code does exactly what you want: applying styles to input[...] selectors:此代码完全符合您的要求:将样式应用于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%);
    }
}

If you want to apply styles to custom classes/ids, consider this approach:如果要将样式应用于自定义类/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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM