8

Here is what I would like to do. Not sure if its possible with LESS CSS, but I have a feeling I just can't find the syntax.

@height : 50px;
@wrap   : 25px;
@bgsize : ((@wrap/@height)*100)+'%';

So that @bgsize == 50%, everything I've tried has cause the script to fail.

1
  • 1
    Whoever down voted me. Care to elaborate? Commented Dec 4, 2011 at 2:05

2 Answers 2

8

Actually, a more elegant way of doing this is just using the percentage() function which is built into LESS.

@height : 50px;
@wrap   : 25px;
@bgsize : percentage(@wrap/@height);
// @bgsize == 50%

Perhaps this was added in a newer version of LESS.

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

3 Comments

I really like this method and will use it in the future, but I am going to leave the green check where it was because ChssPly76 really explained to me why my method was failing, and was much more timely. Your method really is cool though, thanks! You get my upvote.
Yes, this was meant as sort of an addendum to the above answer. I didn't explain why only because ChssPly76 already had.
I don't care about the points or whatever, but shouldn't the more up to date answer show up at the top? Feel free to either edit mine to add the explanation, or edit ChssPly76's to use the more up to date technique. I don't mind either way.
3

AFAIK, expression result will always use the same units as its operands; appending percentage sign to the end will yield something like '50px %' at best or fail altogether.

That said, you can do the following (which is not very elegant, but works):

@height-in-pixels: 50;
@wrap-in-pixels: 25;
@bgsize: @wrap-in-pixels / @height-in-pixels * 100%;
@height: @height-in-pixels + 'px';
@wrap: @wrap-in-pixels + 'px';

You can always avoid the last two lines and "-in-pixels" indirection if you specify units in the actual property definition, too.

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.