Set a variable in Sass depending on the selector

前端 未结 8 1075
失恋的感觉
失恋的感觉 2020-12-10 10:20

I’ve got a website that’s using a few different ‘main’ colors. The general HTML layout stays the same, only the colors change depending on the content.

I was wonderi

相关标签:
8条回答
  • 2020-12-10 11:01

    You can simply override your scss variables inside of the class wrapper:

    $color1: red;
    $color2: yellow;
    
    header { background: $color1; }
    
    .override-class {
      $color1: green;
      header { background: $color1; }
    }
    

    Seems to work for me.

    0 讨论(0)
  • 2020-12-10 11:05

    I think a mixin is the answer. (As I wrote, variables won’t work.)

    @mixin content($color-default, $color-main) {
      background: $color-default;
      color: $color-main;
    }
    
    body.class-1 {
      @include content(#444, #555);
    }
    
    body.class-2 {
      @include content(#666, #777);
    }
    

    That SCSS compiles to this CSS:

    body.class-1 {
      background: #444444;
      color: #555555; }
    
    body.class-2 {
      background: #666666;
      color: #777777; }
    

    If you wanted to group the color values together in your SCSS file, you could use variables in conjunction with the mixin:

    $color-1: #444;
    $color-2: #555;
    $color-3: #666;
    $color-4: #777;
    
    body.class-1 {
      @include content($color-1, $color-2);
    }
    
    body.class-2 {
      @include content($color-3, $color-4);
    }
    
    0 讨论(0)
提交回复
热议问题