How to avoid quotes in a smaller variable that stores the color name?

I am working on an HTML / CSS project. I want to create classes for labels and texts based on color. for example

text-red{
    color: red;
}

label-white{
    color: white;
}

To do this, I am trying to create a mixin that takes a name and color as an argument and creates this class. I wrote the following mixin:

.mixin(@name, @color) {
    .text-@{name} {
        color: @color !important;
    }
    .label-@{name} {
        color: @color !important;
    }
}

.mixin('white', white);

This gives me the following conclusion

.text-'white'{ /* notice the quotes*/
    color: #ffffff
}

If I run this mixin as .mixin (white, white); I get

.text-#ffffff{
    color: #ffffff
}

How to create a class like text-white using mixin?

+4
source share
2 answers

From the link LESS "e" :

e(@string); // escape string content

If you use the function e, you will get the correct result.

.mixin(@name, @color) {
    .text-@{name} {
        color: @color !important;
    }
    .label-@{name} {
        color: @color !important;
    }
}

.mixin(e('white'), white);

, :

@whiteLiteral: e('white');
//mixin declaration code
.mixin(@whiteLiteral, white);
+9

LightStyle , , :

.recursive-loop(@list_size, @list) when (@list_size > 0) {

    // Get the current color from the list @list at index @list_size
    @current_color: e( extract(@list, @list_size) );


    .myclass1-@{current_color} {
        color: @current_color;
    }

    .myclass2-@{current_color} {
        background-color: @current_color;
    }

    //etc...

    // until the end of list
    .recursive-loop( (@list_size - 1), @list)
}

.mixin-color(){

    // Add color you need to this list and get size of it.
    @list: "black", "dimgrey", "grey", "lightgrey", "white", "red", "blue", "green";
    @list_size: length(@list);

    // Call recursive loop with the preview list
    .recursive-loop(@list_size, @list);

}

// mixin call
.mixin-color();

, ...

+1

All Articles