1

When I use the exact same calc() command in a custom css property as a standard property (left), and print it to the console in javascript, the custom property is clearly not calculating correctly like the standard property is. My code is as follows:

.note {
  --qq: calc(10px + 100px);
  left: calc(10px + 100px);
}

When I print these to the console using the following code:

console.log("qq = ",getComputedStyle(qqq).getPropertyValue('--qq'))
console.log("left = ",getComputedStyle(qqq).getPropertyValue('left'))

The result in the console is the following:

qq = calc(10px + 100px)
left =  110px

What am I doing wrong here?

Thanks, and best regards.

Was expecting:

qq = 110px
left = 110px
1

1 Answer 1

-1

You need to create variables in the :root in css, that is why the variable was not being created correctly

:root {
  --qq: calc(10px + 100px);
}
.class {
  left: var(--qq);
}
Sign up to request clarification or add additional context in comments.

12 Comments

This is inside of a class. Will this still work: ``` .note { --qq: calc(10px + 100px); left: calc(10px + 100px); } ```
You should include root and variables at the start of css files, not in a class
There is no reason at all not to set up a variable in a class provided the scope is what you want.
@MrMultiMediator all what is said by "the coder" is simply wrong.
@MrMultiMediator custom properties are treated like "string", as simple as that. You see a calc() but JS see a string. Whatever value you will put inside, the JS will show it like it is.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.