Search code examples
sassscss-lint

SCSS: How to use variables to write style classes ?


I was wondering whether there is a certain way to use variables that affect the style in SCSS.

I'm looking for something like:

var x = 1
.class1 { 
   if (x==1) {
       background-color: red;
   } else { 
       background-color: blue;
   }
}
.class2 { 
   if (x==1) {
       background-color: blue;
   } else { 
       background-color: red;
   }
}

Solution

  • You can use @if and @else

    $x:1;
    
    .class1 { 
       @if $x == 1 {
           background-color: red;
       } @else { 
           background-color: blue;
       }
    }
    .class2 { 
        @if $x == 1 {
           background-color: blue;
       } @else { 
           background-color: red;
       }
    }