Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable in a selector in LESS

Tags:

less

I have a variable @index and I want to output a selector so that when @index is 3, the selector is [data-sth="3"].

Longer example:

@index: 3;
/* selector here */ {
    color: red;
}

Desired output:

[data-sth="3"] {
    color: red;
}

I've tried a few things, but haven't managed to get anything that works, yet.

Thanks.

like image 645
callumacrae Avatar asked Apr 14 '12 15:04

callumacrae


People also ask

How do you represent a variable in LESS?

Defining a variable: Variables in Less are represented with an at (@) sign. We can assign a value using a colon (:).

Can I use CSS variables in LESS?

As we know, less function can't work with css variables.


1 Answers

See Variable Interpolation. I.e.:

[data-sth="@{index}"] {
    color: red;
}

Original answer (Less versions before 1.40):

The following code should work:

(~'[data-sth="@{index}"]') { color: red; }

The ~ removes the quotation marks.

like image 85
callumacrae Avatar answered Dec 02 '22 08:12

callumacrae