In JavaScript, you can get the value of a CSS variable using getPropertyValue(property)
. This function is useful for retrieving variables declared in the :root
block.
:root {
--example-var: 50px;
}
However, if this variable expression contains a function like calc
, the getPropertyValue
call returns the expression as text rather than computing it, even when using getComputedStyle
.
:root {
--example-var: calc(100px - 5px);
}
How can I get the computed value of a CSS variable that uses a CSS function like calc
?
See the example below:
let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
:root {
--example-var: calc(100px - 5px);
}
<div id='example'></div>
calc() The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.
Working of calc() function: The calc() is an inbuilt CSS function, with the purpose to perform calculations while specifying CSS property values. It can be used where a frequency, length, number, time, etc. is allowed. More specifically, the calc() function is used in providing values.
The computed value of a CSS property is the value that is transferred from parent to child during inheritance. It is calculated from the specified value by: Handling the special values inherit , initial , revert , revert-layer , and unset .
To declare a variable in CSS, come up with a name for the variable, then append two hyphens (–) as the prefix. The element here refers to any valid HTML element that has access to this CSS file. The variable name is bg-color , and two hyphens are appended.
Technically you cannot because the computed value is not static and will depend on other properties. In this case it's trivial since we are dealing with pixel value but imagine the case where you will have percentage value. Percentage is relative to other properties so we cannot compute it until it's used with var()
. Same logic if we use unit like em
, ch
, etc
Here is a simple example to illustrate:
let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
console.log(window.getComputedStyle(div).getPropertyValue('font-size'))
console.log(window.getComputedStyle(div).getPropertyValue('width'))
console.log(window.getComputedStyle(div).getPropertyValue('background-size'));
:root {
--example-var: calc(100% + 5px - 10px);
}
#example {
font-size:var(--example-var);
width:var(--example-var);
background-size:var(--example-var);
}
<div id='example'>some text</div>
It's important to note the last case, where background-size has a special behavior when combining percentage and pixel value. You can also clearly see in the first case that the browser will not even compute the 5px - 10px
and will do it only when using var()
.
In case you know that the calc()
will only be used with pixel values, you can simply apply it to any property and read it. It will work since the computed value will always be evaluted the same whataver the property is:
let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
console.log(window.getComputedStyle(div).getPropertyValue('font-size'))
console.log(window.getComputedStyle(div).getPropertyValue('width'))
console.log(window.getComputedStyle(div).getPropertyValue('background-size'));
console.log(window.getComputedStyle(div).getPropertyValue('background-color'));
:root {
--example-var: calc(100px - 10px);
--test:var(--example-var)
}
#example {
font-size:var(--example-var);
width:var(--example-var);
background-size:var(--example-var);
background-color:var(--example-var);
}
<div id='example'></div>
Of course you need to make sure to consider a property that expect a pixel value or you will have an invalid value (like with background in the previous example).
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