Being new to CSS, I like to use CSS custom variables, along with calc, to make my code easily understandable, reusable and modifiable by others, and to keep the page consistent (I don't want the color I use as a main color to be different on two different parts of my page).
However, when I wanted to read via Javascript such a custom variable, which is defined as the sum of 2 other ones, it gave me the plain text "calc(<value of the first variable> + <value of the second one>)"
.
What's more surprising, when I assigned to a standard CSS property the above sum, and then read it, it returned what I was expecting (the result of the sum).
Here's a simplification of my code:
let inputExample1 = document.getElementById("myFirstInput");
let inputExample2 = document.getElementById("mySecondInput");
inputExample1.value = getComputedStyle(inputExample1).getPropertyValue("--a-local-custom-variable");
inputExample2.value = getComputedStyle(inputExample2).getPropertyValue("top");
:root{
--a-custom-variable: 10px;
--another-custom-variable: 20px;
--a-result-of-the-previous-variables: var(--a-custom-variable) + var(--another-custom-variable);
}
input {
--a-local-custom-variable: calc(var(--a-result-of-the-previous-variables));
top: calc(var(--a-result-of-the-previous-variables));
}
<html>
<head>
<link href="Custom_CSS_variable_test.css" rel="stylesheet">
</head>
<body>
<input id="myFirstInput" type="text">
<input id="mySecondInput" type="text">
</body>
</html>