Set a variable in Sass depending on the selector

前端 未结 8 1074
失恋的感觉
失恋的感觉 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 10:40

    If you don't want to use a variable for each color, you can use one variable for all kinds of colors. In the mixin you can choose the right color with nth. For instance, if you write the index of the color as 1, then you get the first color in the color variable.

    $colors: #444, #555, #666, #777;
    
    @mixin content($color-default-num, $color-main-num) {
      background: nth($colors, $color-default-num);
      color: nth($colors, $color-main-num);
    }
    
    body.class-1 {
      @include content(1, 2);
    }
    
    0 讨论(0)
  • 2020-12-10 10:43

    If you really want to get hacky you could also define your different color schemes in a single variable like $scheme1: class1 #333 #444, where the first value is always the name, and that is followed by all the colors in that scheme.

    You can then use @each:

    // Define your schemes with a name and colors
    $scheme1: class1 #444 #555;
    $scheme2: class2 #666 #777;
    $scheme3: class4 #888 #999;
    
    // Here are your color schemes
    $schemes: $scheme1 $scheme2 $scheme3;
    
    @each $scheme in $schemes {
      // Here are the rules specific to the colors in the theme
      body.#{nth($scheme, 1)} .content {
        background-color: nth($scheme, 2);
        color: nth($scheme, 3);
      }
    }
    

    This will compile to:

    body.class1 .content {
      background-color: #444444;
      color: #555555; }
    
    body.class2 .content {
      background-color: #666666;
      color: #777777; }
    
    body.class4 .content {
      background-color: #888888;
      color: #999999; }
    

    Obviously if you don't want to combine body.class1 and .content in your selectors, you could just specify a mixin content($main, $default) and call it inside the @each using nth just like in the above code, but the point is you don't have to write out a rule for each of your classes.

    EDIT There are lots of interesting answers on Creating or referencing variables dynamically in Sass and Merge string and variable to a variable with SASS.

    0 讨论(0)
  • 2020-12-10 10:49

    UPDATE: its 2017 and variables does works!

    @mixin word_font($page) {
      @font-face {
        font-family: p#{$page};
        src: url('../../static/fonts/ttf/#{$page}.ttf') format('truetype');
        font-weight: normal;
        font-style: normal;
      }
    
      .p#{$page} {
       font-family: p#{$page};
      }
    }
    
    // Loop and define css classes 
    @for $i from 1 through 604 {
     @include word_font($i);
    }
    
    0 讨论(0)
  • 2020-12-10 10:51

    You can also create a mixing that use the ampersand parent selector. http://codepen.io/juhov/pen/gbmbWJ

    @mixin color {
      body.blue & {
        background: blue;
      }
      body.yellow & {
        background: yellow;
      }
    }
    
    0 讨论(0)
  • 2020-12-10 10:52

    For me the definite answer to my problem was creating a map of maps and loopig through them as follows:

    $pallettes: (
      light-theme: (
        container-color: red,
        inner-color: blue,
      ),
      dark-theme: (
        container-color: black,
        inner-color: gray,
      ),
    );
    
    @each $pallette, $content in $pallettes {
      .main.#{$pallette} {
        background-color: map-get($content, container-color);
        .inner-div {
          background-color: map-get($content, inner-color);
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-10 10:59

    as sass documentation explain nicely (https://sass-lang.com/documentation/variables):

    • Sass variables are all compiled away by Sass. CSS variables are included in the CSS output.

    • CSS variables can have different values for different elements, but Sass variables only have one value at a time.

    • Sass variables are imperative, which means if you use a variable and then change its value, the earlier use will stay the same. CSS variables are declarative, which means if you change the value, it’ll affect both earlier uses and later uses.

    We may take advantage of that using a combination of sass and css variables to achieve what you want:

    //theme colors
    $red-cosmo: #e01019;
    $green-cosmo: #00c398;
    $primary-color: var(--primary-color);
    body{
      --primary-color: #{$red-cosmo};
    }
    body.univers-ride{
      --primary-color: #{$green-cosmo};
    }
    

    So when I call my sass variable $primary-color, it will print as my css variable "var(--primary-color)" that will expand as $green-cosmo only if my body has the "univers-ride" class else it will be $red-cosmo the default color.

    0 讨论(0)
提交回复
热议问题