I will calculate width in some element from percent to pixel so I will minus -10px via using LESS and calc(). It´s possible?
div { span { width:calc(100% - 10px); } }
I using CSS3 calc()
so it doesn't work: calc(100% - 10px)
Example: if 100% = 500px so width = 490px (500-10);
I made a demo for testing : http://jsfiddle.net/4DujZ/55/
so padding will say: 5 (10px / 2) all the time when I resizing.
Can I do it in LESS? I know how to do in jQuery and simple CSS like margin padding or else... but i will try to do functional in LESS with calc()
With pixels, it is easy to position objects relative to each other and controls specific heights and widths. On the other hand, scaling objects with percentages is easy. % Is the way to go in a modern world like vector graphics. When the screen is large or small you can resize it exactly regardless of resolution.
CSS calc() It can be used to calculate length, percentage, time, number, integer frequency, or angle. It uses the four simple arithmetic operators add (+), multiply (*), subtract (-), and divide (/). It is a powerful CSS concept because it allows us to mix any units, such as percentage and pixel.
The calc() function takes a single expression as its parameter, with the expression's result used as the value. The expression can be any simple expression combining the following operators, using standard operator precedence rules: + Addition.
You can escape the calc
arguments in order to prevent them from being evaluated on compilation.
Using your example, you would simply surround the arguments, like this:
calc(~'100% - 10px')
Demo : http://jsfiddle.net/c5aq20b6/
I find that I use this in one of the following three ways:
Everything inside the calc
arguments is defined as a string, and is totally static until it's evaluated by the client:
div { > span { width: calc(~'100% - 10px'); } }
div > span { width: calc(100% - 10px); }
You can insert a LESS variable into the string:
div { > span { @pad: 10px; width: calc(~'100% - @{pad}'); } }
div > span { width: calc(100% - 10px); }
You may want to escape a percentage value, but go ahead and evaluate something on compilation:
@btnWidth: 40px; div { > span { @pad: 10px; width: calc(~'(100% - @{pad})' - (@btnWidth * 2)); } }
div > span { width: calc((100% - 10px) - 80px); }
Source: http://lesscss.org/functions/#string-functions-escape.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With