Deepak Yadav
Deepak Yadav

Reputation: 7069

Use mixin argument to create class name in LESS

I'm trying to create a simple mixin in LESS for different colors I'll use for a website.

What i want is use mixin argument as a part of class name as well.

@green: #5FBEAA; // my color variable

.text-color(@color) {
    .text-{@color} {
        color: @color;
    }
}

.text-color(@green);

The output i'm getting is:

.text-#5FBEAA {
  color:#5FBEAA
}

What I want is:

.text-green {
  color:#5FBEAA
}

Upvotes: 4

Views: 1898

Answers (2)

user4994625
user4994625

Reputation:

I think I have the solution using Variable Names.

Less

@green: #5FBEAA;

.text-color(@colorname) {
  @color: ~"@{colorname}";
  .text-@{color}{
    color: @@color;
  }
}

.text-color(green);

Output

.text-green {
  color: #5FBEAA;
}

Upvotes: 6

Jinu Kurian
Jinu Kurian

Reputation: 9416

I don't think its possible. The closest solution for this will be using an additional variable.

@green: #5FBEAA;
 .text-color(@name,@color) {
     .text-@{name} {
         color: @color;
     }
 }
 .text-color(green,@green);

This will compile to

.text-green {
  color: #5FBEAA;
}

Upvotes: 2

Related Questions