Creating or referencing variables dynamically in Sass

后端 未结 6 1770
臣服心动
臣服心动 2020-11-22 01:31

I\'m trying to use string interpolation on my variable to reference another variable:

// Set up variable and mixin
$         


        
6条回答
  •  温柔的废话
    2020-11-22 01:48

    I came across the need to reference a colour dynamically recently.

    I have a _colours.scss file for every project, where I define all my colours once and reference them as variables throughout.

    In my _forms.scss file I wanted to setup button styles for each colour available. Usually a tedious task. This helped me to avoid having to write the same code for each different colour.

    The only downside is that you have to list each colour name and value prior to writing the actual css.

    // $red, $blue - variables defined in _colours.scss
    $colours: 
      'red' $red,
      'blue' $blue;
    
    @each $name, $colour in $colours {
      .button.has-#{$name}-background-color:hover {
        background-color: lighten($colour, 15%);
      }
    }
    

提交回复
热议问题