Pass string into variable in sass mixin

前端 未结 1 1430
执念已碎
执念已碎 2021-01-28 21:55

I have a very simple mixin which looks like this:

@mixin global( $variable-name ) {
    font-size:   #{$variable-name}-font-size;
}

I have prev

相关标签:
1条回答
  • 2021-01-28 22:31

    You can't create a dynamic variables in sass.

    '#{}' means it will convert whatever attribute to its plain css form, it won't be treated as a variable it will be treated as a text.

    What you can do is create a map for the list of properties and call them inside the mixin.

    $input-font-size: 16px;
    $textarea-font-size: 14px;
    
    
    $var-map: (
      input: $input-font-size, 
      textarea:  $textarea-font-size,
    );
    
    @mixin global( $variable-name ) {
        font-size:  map-get($var-map, $variable-name);
    }
    
    body {
      @include global( input );
    }
    

    or if you dont want to create the map then you can simply pass the variable name in the mixin

    @mixin sec( $variable-name ) {
      font-size: $variable-name;
    }
    .text-area {
      @include sec( $textarea-font-size );
    }
    

    Sample pen https://codepen.io/srajagop/pen/aWedNM

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