Accessing the name of the variable rather than its value

前端 未结 4 1181
遥遥无期
遥遥无期 2021-01-14 12:56

I am trying to write a loop that will cycle through colors and condense the amount of code in my scss file. Here is a simple example of what I have:

$color1:         


        
4条回答
  •  迷失自我
    2021-01-14 13:54

    This is more of a comment on the previous answers. It's possible to combine both answers, which results in pretty elegant code (in my not-so-humble opinion).

    I use Dutch color names, I do so on purpose as to make clear we're not dealing with HTML color names.

    $rood      : #e3004a;
    $blauw     : #009ee0;
    
    @each $color in ('rood', $rood), ('blauw', $blauw) {
    
        $name: nth($color, 1);
        $value: nth($color, 2);
    
        .#{$name} {
            a{
                background-color: $value;
            }                   
        }
    }
    

    CSS

    .rood a { background-color: #e3004a; }
    
    .blauw a { background-color: #009ee0; }
    

提交回复
热议问题