Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCSS Variables as @extend class

Tags:

css

sass

My idea is that I would like to write silent classes for input[type=text], input[type="password"] and input[type=submit]. I would then @extend them in a mixin by passing hem through as a variable.

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

like image 883
Kevin Lewis Avatar asked Jul 09 '13 10:07

Kevin Lewis


People also ask

What does @extend mean in SCSS?

Sass @extend Directive The @extend directive lets you share a set of CSS properties from one selector to another. The @extend directive is useful if you have almost identically styled elements that only differ in some small details.

Can you change SCSS variable value dynamically?

CSS Variables to the Rescue CSS variables is a standard which enables you to have variables in CSS. SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time.

Can I use SCSS variables in CSS?

To convert the SASS variable to a CSS custom property you put curly brackets around it, and a hash in front. If you've used template literals in JavaScript it's the same thing, just with a # instead of a $ (because we already have $ in the variable name).

Can you use SCSS variables in JS?

To make Sass (or, specifically, SCSS in this case) variables available to JavaScript, we need to “export” them. The :export block is the magic sauce webpack uses to import the variables. What is nice about this approach is that we can rename the variables using camelCase syntax and choose what we expose.


2 Answers

try using variables interpolation

@extend #{$type};

Further information on SASS Reference

like image 199
Fabrizio Calderan Avatar answered Nov 14 '22 03:11

Fabrizio Calderan


While Fabrizio's answer is formally correct, consider not going that way.

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. 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 {
    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:

/////////////////
// 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/

like image 35
Andrey Mikhaylov - lolmaus Avatar answered Nov 14 '22 04:11

Andrey Mikhaylov - lolmaus