LESS Declare variables using class names?

前端 未结 1 764
刺人心
刺人心 2021-01-24 11:09

I have a button class that sets up padding etc for an element, followed by a class that defines a background colour.

.button {
    padding: 0.5em 1em;
    text-t         


        
相关标签:
1条回答
  • 2021-01-24 11:33

    You could achieve this by having a variable with the required list of colors, a loop to create the required rules and selector interpolation like shown below.

    @colors: "green","blue","orange","red","yellow"; // the list of colors required
    button {
        padding: 0.5em 1em;
        text-transform: uppercase;
        .loop-colors(@index) when (@index > 0){ // loop to generate rules for each color
            .loop-colors(@index - 1); // call for the next iteration
            @color: e(extract(@colors, @index)); // pick the color value from the list one by one based on current index
            &.@{color} {
                background:@color;
            }
        }
        .loop-colors(length(@colors));
    }
    

    Codepen Demo

    Note: As mentioned in comments, LESS PHP is quite outdated and hence some of the new features offered by LESS (with respect to loops) are not supported by it. It could be overcome by doing the work-around mentioned in this answer.

    You could also adopt an approach similar to the one mentioned by seven-phases-max in this answer (2nd option). That one achieves a similar effect without using loops.

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