1

I am converting LESS to CSS, there I want to run the LESS function below:

.myPL( @val ) {
   .pL @val{
    padding-left:@val;
   }
}

Function Call:

.myPL( 20px );

Expected result:

.pL20px{padding-left:20px}

But actual result is Syntax Error.

Please help me to concatenate the strings in class name in LESS.

0

3 Answers 3

8

What you are looking for is called selector interpolation ... you can find it here: http://lesscss.org/#-selector-interpolation

Your mixin would need to look like this for it to work:

.myPL( @val ) {
  .pL@{val} {
    padding-left: @val;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1, although I still cannot think of a situation when this is a good idea!
What if @val is a number? Could you do .pL@{val*2} ?
@bcackerman the best is to separate arithmetics out of the string interpolation ... e.g. first do @val2: @val * 2; and then use the new val in .pL-@{val2}. And in the properties you can then also add an unit to the number with unit().
1

What you are trying to achieve does not work in LESS:

You could do:

.myPL( @val ) {
   padding-left: @val;
}

1 Comment

Thanks For the help. What i am trying to do here is, a common function for padding left ranging from pL5, pL10,..., pL40.
1

Why on earth would you manually define each possible variant of padding left with the classname itself? That's not what LESS was designed for, and doesn't really make much sense with the context you've given.

The idea of mixins is to make them reusable, but I can't understand why you'd call a classname in the middle of that mixin. Use LESS mixins properly, and do the following:

.pl(@val) {
    padding-left: @val;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.