1

I have a CSS variable called --menuWidth. I'm trying to use this variable with an arithmetic operator but I'm having trouble.

I tried the following:

left: calc(var(--menuWidth) + 20px);

But the less processor output says "OperationError: Operation on an invalid type" (at column 2?)

I found this working codepen which actually seems to confirm the syntax above, so I'm assuming this is a LESS issue. If so, how can I fix it?


Here is my file structure:

vars.less

:root {
    --menuWidth: 200px;
}

@media screen and (min-width: 1280px) and (max-width: 1919px){
    :root {
        --menuWidth: 250px;
    }
}
@media screen and (min-width: 1920px){
    :root {
        --menuWidth: 300px;
    }
}

layout.less

#headerContainer {
    left: calc(var(--menuWidth) + 20px);
}

compiled.less

@import "vars.less";
@import "layout.less";
5
  • To be clear, I have already removed the operation and set a left-margin of 20px which I know fixes the issue, but I want to know how to do this for future usage. Commented Jun 18, 2018 at 15:52
  • 1
    CSS variables aren't interoperable with LESS. Commented Jun 18, 2018 at 15:53
  • 1
    Update to Less v3 or higher or (if you have to use earlier versions) see stackoverflow.com/questions/50913203. Commented Jun 19, 2018 at 6:17
  • You linked to this question. :) Commented Jun 19, 2018 at 13:32
  • 1
    My bad, stackoverflow.com/questions/11972084 or any of stackoverflow.com/search?q=%5Bless%5D+calc. Commented Jun 19, 2018 at 14:19

1 Answer 1

1

Updating LESS from 2.7.2 to 3.0.4 fixed the issue.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.