The answer to the question is "Yes, you can create and work with arrays in Less." An array (in CSS, and therefore, to a lesser extent, is usually called a “list”) is defined using the same code as in your Q:
@badge-colors: blue
- See "List functions" for functions to help you iterate over such lists / arrays and access their elements.
- Also see this answer for some additional / less obvious properties of Less lists.
- . " " //.
Less ( ) CSS:
@colors: blue, green, yellow, red;
// mixin to iterate over colors and create CSS class for each one
.make-color-classes(@i: length(@colors)) when (@i > 0) {
.make-color-classes(@i - 1);
@color: extract(@colors, @i);
.@{color} {
color: @color;
}
}
.make-color-classes(); // run the mixin
- . " " "" ( /, .. "") .
"Modern Less" ( less-plugin-lists .for-each):
@badge-colors:
blue
gray
green
red
.for-each(@pair in @badge-colors) {
@key: at(@pair, 1);
.badge-@{key} {
color: at(@pair, 2);
}
}
"Legacy Less" ( ):
// usage:
@badge-colors: blue
.make-classes(badge, @badge-colors);
// impl.:
.make-classes(@prefix, @list) {
.iter(length(@list));
.iter(@i) when (@i > 0) {
.iter(@i - 1);
@pair: extract(@list, @i);
@key: extract(@pair, 1);
@value: extract(@pair, 2);
.@{prefix}-@{key} {
color: @value;
}
}
}
, , /, , SO: