Can numbers be rounded (math term) using LESS CSS?
18,869
Solution 1
Yes they can:
line-height: ceil(@height * .666); // 20px (round up)
line-height: floor(@height * .666); // 19px (round down)
line-height: round(@height * .666); // 20px (round to closest integer)
line-height: round(@height * .666, 1); // 20.0px (round to 1 decimal place)
Solution 2
http://lesscss.org/functions/ for a list of the lesscss functions, floor is included under the heading "Math functions".
Related videos on Youtube
Author by
Kirkland
I am husband to a gorgeous wife, father to three genius boys, a relentless entrepreneur, business leader, aspiring philanthropist, and a software engineer with an undeniable addiction to the Internet.
Updated on September 18, 2022Comments
-
Kirkland over 1 year
LESS CSS = http://lesscss.org/
I declared a variable, like this...
@height: 30px
Then I used a simple calculation, like this...
line-height: @height * .666
It returns
19.98px
but I wanted an even20px
So, does LESS CSS have a way to round numbers up or down?
-
Paul almost 11 years@Greg LESS is a language that compiles to CSS. It has nothing to do with jQuery.
-
Paul almost 11 years@Greg Yeah, the official compiler is written in Javascript. There is also a popular compiler written in PHP. It still has nothing to do with jQuery and even the Javascript compiler doesn't use jQuery.
LESS
itself is a language. Just likeJavascript
,CSS
,C++
, andHTML
are all languages.
-
-
Kirkland almost 11 yearsThanks @Paulpro I'll accept this answer as soon as it allows. Do you have any resources (aka: links) you can recommend for other LESS "tricks"? I'm new to LESS and I didn't see much on the official site.
-
Paul almost 11 years@Kirkland You can always just scan the reference (link) for a function that suits your needs:
-
Greg almost 11 years@Paulpro: Math.ceil(number) is a javscript function.msdn.microsoft.com/en-us/library/ie/w0w5b52h%28v=vs.94%29.aspx as are the others.
-
Paul almost 11 years@Greg ceil is also in
C
,C#
,C++
,PHP
, and a whole bunch of other languages, includingLESS
. -
Paul almost 11 years@Greg You may also notice that the
round
function in Javascript doesn't take in a second argument to specify the number of decimal places, like theround
in LESS does. -
Paweł over 8 yearsis it possible without preprocessors like LESS? so just CSS...?