14

The variable can take percentage or px values, like:

@some-var: 50px; or @some-var: 46%;

How can I define a certain set of CSS rules if the value is in pixels, and a different set of rules if the values is in percentages?

Is there something like

if(isValueInPixels(@some-var)){

  // css rules here


}else{

  // other rules here
}

?

0

2 Answers 2

16

I think you can use something that they call Guarded Mixins.

Try something like this...

.mixin (@a) when (ispixel(@a)) {
 /* ... your pixel specific logic ... */
}
.mixin (@a) when (ispercentage(@a)) {
 /* ... your percentage specific logic ... */
}

.coolStuff {
 .mixin(50px);
 .mixin(50%);
}

See the Guarded Mixins at http://lesscss.org/

Sign up to request clarification or add additional context in comments.

1 Comment

I found what I needed in addition to this answer was a default which is possible using default() function. From the docs at lesscss.org/features/#mixin-guards-feature-conditional-mixins .mixin (@a) when (default()) { ... }
7

As Adam Spicer noted the Guarded Mixins are your best solution. However, LESS now offers a solution to group multiple guards, which will allow this to be accomplished with a single mixin. http://lesscss.org/features/#css-guards-feature

For Example:

.mixin(@a){
  & when (ispixel(@a)){
    //logic
  }
  & when (ispercentage(@a)){
    //logic
  }
}

Usage:

selector{
  .mixin(50px);
  .mixin(46%);
}

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.