CSS3 calc() function doesn't work with LESS
I know it's been reported already but bugs were closed due to insufficient information.
I have the following CSS code:
#id1 {
position: fixed;
left: 0; top: 0;
width: 130px;
}
#id2 {
position: fixed;
right: 0; top: 0;
width: calc(100% - 130px);
}
I wanted to transform it into LESS using 130px as a parameter but calc gets internpreted by LESS and this:
@id1width: 130px
#id1 {
position: fixed;
left: 0; top: 0;
width: @id1width;
}
#id2 {
position: fixed;
right: 0; top: 0;
width: calc(100% - @id1width);
}
makes the last but one line transformed to width: calc(-30%); which is clearly not what's desired. I tried using width: ~"calc(100% - @id1width)"; but it makes @id1width not interpreted.
I think LESS shouldn't use things reserved by CSS3 for its internal use...
they were probably closed as duplicates.. though I can't find one about calc.. see #146 #122 and #915
workaround: width: ~"calc(100% - @{id1width})"; - note curly brackets around variable.
We are moving to a system where only things inside brackets will be evaluated to fix this problem.
:+1:
@rows: 10;
@row1: 1 / @rows * 100%;
.featured1
{
top: ~'-webkit-calc(@{row1} + 20px)';
right: 0;
bottom: @row5;
left: @col9;
}
This bit of LESS will process out the value of '@row1' to be 10%, which is great. But when it is inside an escaped seciton of LESS and wrapped in curlys to retain the LESS variable it returns '10' without the '%'.
I've found a workaround that hasn't failed me yet. If you place another '%' after the closing curly of the 'row1' variable inside the escaped code it will work correctly...
~'-webkit-calc(@{row1}% + 20px)';
But that seems like quite a hack to add in another unit type that is suppose to already be in the variable.
@jonjohnjohnson this is fixed in head and will be in 1.3.2
the rest of this bug will be resolved in 1.4.0
Not sure if this is the same issue, but I'm having a problem with the following. Note there's no less-specific stuff here, less 1.3.3 is munging otherwise valid CSS.
Here's the CSS
html, body {
margin:0, padding:0, border:0;
height: 100%;
}
#content {
height: -webkit-calc(100% - 40px);
height: calc(100% - 40px);
background-color:gray;
}
#footer {
background-color:black;
}
And here's the HTML to include it
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet/less" type="text/css" href="test.css" />
<script src="less-1.3.3.min.js"></script>
<body>
<div id="content"></div>
<div id="footer" style="height:40px"></div>
</body>
</html>
"content" is being set to a height of 60%, so less is parsing & incorrectly resolving the calc expression rather than passing it unchanged through to the browser. Tested in Safari 6.0.2 and Firefox.
Fixed on master for 1.4.0
height: ~"calc(100% - 50px)";
still produces:
height: calc(50%);
for me. I want:
height: calc(100% - 50px);
What’s more, it still produces height: calc(50%); even with strict math set to on.
@OliverJAsh I can't reproduce your results with Less 1.7.0 (both escaped value and --strict-math:on work as expected)... Could you please provide more details on the compiler/environment/scripts you use? (Just in case there was Bootstrap build script issue that could lead to such incorrect results: https://github.com/twbs/bootstrap/issues/13604, maybe this is your case too?).
I was having this issue in 1.6.3 (for some reason, WinLESS is barfing on compile when I upgrade to 1.7.x, so I'm sticking to 1.6.x for now)
My quickfix was just to escape one part of the equation like:
height: calc(~"100%" - unit(@var, px));
This even works for variables, like @var: 50. Or you could escape the whole calculation like calc(~"100% - 50px");
@twiginteractive If you have to use escaping with --strict-math option off (default setting) then it's not an issue but expected behaviour. See --strict-math.
@seven-phases-max Hmmm - according to the docs, with SM off (default) then this should get parsed correctly (i.e. untouched)
height: calc(100% - 10px);
But it doesn't. The output CSS is height: calc(90%);, which isn't the desired result. Maybe this is fixed in 1.7, but as I say I can't use that version right now until I figure out what is breaking the WinLESS compile.
(Unless I am mis-reading the docs when it says "will be processed correctly"... LESS couldn't know the value of 100%, so it shouldn't do a mathematical computation on '100% - 10px')
@twiginteractive
according to the docs, with SM off (default) then this should get parsed correctly (i.e. untouched)
No, the word there is currently not correctly (i.e. "will be processed currently").
@seven-phases-max Ah - my bad, it makes sense now. Thanks for the clarification.
height: ~"calc(100% - 50px)"; Worked for me.
~~This is where Less.js begins to mess with your code. Supposedly v2.0 will have the option to prefix its functions like so:~~
height: _calc(50%);
height: calc(100% - 50%); /* browser-native */
@stevenvachon
Supposedly v2.0 will have the option to prefix its functions like so:
This won't affect calc in any way. There's no built-in calc function, Less is just evaluating a math expression accordingly to its (Less's) rules. That's it. If you don't want this "mess" use --strict-math.
P.s. the solution to this problem is strict maths. And maybe we should force strict maths on when in a call to calc?
~~The problem extends when using Myth after Less. Even with strictMath, we need to ~"calc(...)" so that Less ignores it.~~
Why? Does myth remove parentheses? Strict math makes less a proper superset of css.