Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS CSS computation

Tags:

css

less

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.

like image 868
Fresheyeball Avatar asked Dec 04 '11 01:12

Fresheyeball


People also ask

How do you write less in CSS?

Create a stylesheet with . less extension and link it in your document using the rel="stylesheet/less" attribute. You are all set and can compose styles within the . less .

What is less CSS framework?

Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less. js, the JavaScript tool that converts your Less styles to CSS styles. Because Less looks just like CSS, learning it is a breeze.

What is the lesser function?

The Excel MIN function returns the smallest numeric value in the data provided.


2 Answers

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.

like image 179
fletom Avatar answered Sep 27 '22 17:09

fletom


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.

like image 40
ChssPly76 Avatar answered Sep 27 '22 18:09

ChssPly76