2
votes

I have a variable which have empty string as default value and it can be changed dynamically based on skin name. So I tried to write my sass selector like this.

default.scss:

$skin: '';

green.scss:

$skin: 'green';

main.scss:

@import 'default.scss';
.#{$skin} .header{
    color: black;
}

So I expected the below output from generated CSS.

.header{
  color: black;
}

and

.green .header{
   color: black;
}

But it throws below error while compiling Invalid CSS after ".": expected class name, was ".header"

1
when $skin is empty your output is . .header so you could move the dot inside the variableFabrizio Calderan
@fcalderan: Thanks for the update. Is it possible to do this kind of requirement?Yahwe Raj

1 Answers

3
votes

You could use a function for this:

$skin: 'green';
@function skin(){ 
    @if $skin != '' { @return '.' + $skin + ' '; } 
    @else { @return ''; }
}

Now you could use it like this:

#{skin()} .header { ... }

Which will output:

.green .header { display: block; }

It might be better to use a better global variable name (skin seems like something that might quickly be reused - I would suggest something like $global-skin-name or something, but thats just semantics).