Dynamic Sass Variables

后端 未结 3 2045
执笔经年
执笔经年 2020-11-27 08:08

Is there any way I can set my color variables depending on what class is on the html element? Or any other way to achieve this same goal?

html {

  &.sun         


        
3条回答
  •  渐次进展
    2020-11-27 08:35

    This is basic theming. You would either want to use a mixin or include to do multiple themes in a single CSS file. This is how you would go about it using includes:

    _theme.scss

    section.accent {
        background: $accent;
    }
    
    .foo {
        border: $base;
    }
    
    .bar {
        color: $flat;
    }
    

    main.scss

    html {
      &.sunrise {
        $accent: #37CCBD;
        $base: #3E4653;
        $flat: #eceef1;
    
        @import "theme";
      }
    
      &.moonlight {
        $accent: #18c;
        $base: #2a2a2a;
        $flat: #f0f0f0;
    
        @import "theme";
     }
    }
    

    You could just as easily make a mixin that takes 3 colors as its arguments to use in place of the include:

    @mixin theme($accent, $base, $flat) {
        // .. do stuff ..
    }
    

提交回复
热议问题