You cannot "read" the name of the selector.
However, you can create a selector in combination with a link to the file name in mixin, something like this:
LESS
.buildSelectorConnection(@selectorName, @pre: ~'') { @{pre}@{selectorName} { background-image: url('images/@{selectorName}.png'); } } .buildSelectorConnection(header, ~'#'); .buildSelectorConnection(do-a-class, ~'.');
CSS output
#header { background-image: url('images/header.png'); } .do-a-class { background-image: url('images/do-a-class.png'); }
However, you will need a bit more logic, decision making on your part and some javascript codes in LESS if you want this thing to be able to handle something like div#menu ul li , where the actual file name was something like div-menu-ul-li.png .
Nevertheless...
Something like this can be done:
LESS
.buildSelectorConnection(@selectorName, @pre: ~'', @post: ~'') { @{pre}@{selectorName}@{post} { background-image: url('images/@{selectorName}.png'); } } .buildSelectorConnection(menu, ~'div#', ~' ul li');
CSS output
div#menu ul li { background-image: url('images/menu.png'); }
This basically allows you to create any selector line, but the file name itself will only consist of the initial selector passing, which should be something valid for the file name.
ScottS
source share