Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since a counter based solution seems to still may have some shortcomings depending on possible use-cases (beside the hack-based counter thing itself, see my <a href="https://stackoverflow.com/a/20199775/2712740">comment</a> to the corresponding answer) I decided to post a list/loop-based solution I mentioned earlier. I keep the code here as close as possible to the counter-based one so they could be easily compared. (But in general all could be made much clean, structured and generic with further polishing by renaming and reordering all those namespaces/selectors/mixins/variables, removing unnecessary quotes etc.).</p> <h2>Opt. 1</h2> <p>When you need only arbitrary icon(s) of the sprite to have its class in the CSS output:</p> <pre class="lang-css prettyprint-override"><code>@row: 1; // ...... .my-icon-bundle { .myIcon(@row, @index) { @x: (@row * @index); @y: (@row * @index); background-position: -@x -@y; } .myIconX(@name) { @icons: "classX1", "classYY1", "classZZZ", "anotheRR9", "etc."; .find(1); .find(@i) when (@name = extract(@icons, @i)) { @name_: e(@name); .my-icon-@{name_} { #my-icon-bundle.myIcon(@row, @i); } } .find(@i) when not (@name = extract(@icons, @i)) { .find((@i + 1)); } } } // ...... // usage: #my-icon-bundle.myIconX("anotheRR9"); #my-icon-bundle.myIconX("classX1"); </code></pre> <h2>Opt. 2</h2> <p>When you just need to generate corresponding classes for all icons in the sprite:</p> <pre class="lang-css prettyprint-override"><code>@row: 1; // ...... #my-icon-bundle { .myIcon(@row, @index) { @x: (@row * @index); @y: (@row * @index); background-position: -@x -@y; } .icons() { @icons: "classX1", "classYY1", "classZZZ", "anotheRR9", "etc."; .make(length(@icons)); .make(@i) when (@i &gt; 0) { .make((@i - 1)); @name_: e(extract(@icons, @i)); .my-icon-@{name_} { #my-icon-bundle.myIcon(@row, @i); } } } } // ...... // usage: #my-icon-bundle.icons(); </code></pre> <hr> <p>P.S. All this is for LESS 1.5.x, I'm too lazy to make it compatible with earlier versions - sorry.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload