Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to generate CSS with loop in less

How to generate CSS with loop in less

Problem

I am not familiar with Less. In my understanding, I think Less can transform the less format file to standard css file(if I am wrong, please correct me). Now I have a question below.

Say you would generate 100 CSS classes like below(from .span1 to .span100) in a single CSS file. I want to know whether less can generate a CSS file like it?

...
.span5 {
  width: 5%;
}

.span4 {
  width: 4%;
}

.span3 {
  width: 3%;
}

.span2 {
  width: 2%;
}

.span1 {
  width: 1%;
}
Problem courtesy of: Joe.wang

Solution

All, I found a way to output css in loop. pleae review it .thanks.

@iterations: 100;

// helper class, will never show up in resulting css
// will be called as long the index is above 0
.loopingClass (@index) when (@index > 0) {

    // create the actual css selector, example will result in
    // .myclass_30, .myclass_28, .... , .myclass_1
    (~"[email protected]{index}") {
        // your resulting css
        width: percentage((@index - 1) *0.01);
    }

    // next iteration
    .loopingClass(@index - 1);
}

// end the loop when index is 0
.loopingClass (0) {}

// "call" the loopingClass the first time with highest value
.loopingClass (@iterations);
Solution courtesy of: Joe.wang

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

How to generate CSS with loop in less

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×