Conditional statements with SCSS

11,484

I believe you are looking for something like this?

@each $class in purple, none {
  $brand: #00cccc;
  $selector: 'body';
  @if $class == purple {
    $brand: #ff0033;
    $selector: 'body.' + $class;
  }
  #{$selector}{
    /* Use $brand */
  }
}
Share:
11,484

Related videos on Youtube

Andrew Lazarus
Author by

Andrew Lazarus

I specialise in web design and Pure Logic (The Form type), Moonlighting as a story telling artist, creating abstract surreal shorts using a variety of live footage, CGI creations and traditional animation techniques. I also develop pixel-orientated games & websites with my dear friend @forsvunnet.

Updated on June 04, 2022

Comments

  • Andrew Lazarus
    Andrew Lazarus almost 2 years

    Does anyone know if this is possible:

    I want my brand colour to be $brand: #00cccc;

    however, I want to change that on just one page, and that page is defined by a class on it's body.

    body class="purple"
    

    Now my mind is thinking that this would be ideal:

    body.$class { 
        @if $class == 'purple'
        { 
            $brand = #ff0033;
        }
        @else 
        {
            $brand = #00cccc;
        }
    }
    

    But that's not correct syntax at all.

    Is this something that can be done in a similar way?

  • Andrew Lazarus
    Andrew Lazarus about 5 years
    This would mean redeclaring all the styles again, effectively, reducing my love for scss