Can I use CSS calc within Javascript?

There are some interesting things that happen when you use calc with the same type of units, e.g. 10px + 5px. It gets simplified to 15px by the process that puts it onto the element.

So, to expand on rounin's great answer, here's some examples of that behaviour in action:

function growDiv(e) {
        const thisDiv = e.target;
        const x = 100;
        const y = 42;
        const z = 69;
        let widthVal;

        if (thisDiv.id == "simplifies") {
          widthVal = `calc(${y + z}px + ${x}px + ${y}px)`;
        } else if (thisDiv.id == "mixed-units") {
          widthVal = `calc(0em + ${y + z}px + ${x * 2}px + ${y}px)`;
        } else if (thisDiv.id == "variables") {
          thisDiv.style.setProperty("--x", x + "px");
          thisDiv.style.setProperty("--y", y + "px");
          thisDiv.style.setProperty("--z", z + "px");
          widthVal = "calc((var(--x) * 2) + var(--y) + (var(--z) * 2))";
        }

        thisDiv.style.width = widthVal;
        thisDiv.innerHTML =
          `input: ${widthVal}<br>style:${thisDiv.style.width}`;
      }

      document
        .querySelectorAll("div")
        .forEach((el) => el.addEventListener("click", growDiv, false));
.inner-div {
        background-color: hotpink;
        color: white;
        font-weight: bold;
        height: 100px;
        margin-bottom: 5px;
        text-align: center;
        transition: all 0.5s linear;
        width: 100px;
      }
<div class="inner-div" id="simplifies">simplifies<br />1) Click Me</div>
<div class="inner-div" id="mixed-units">mixed-units<br />2) Click Me</div>
<div class="inner-div" id="variables">variables<br />3) Click Me</div>
    

Div 1 has all the same units, and therefore simplifies.

Div 2 has a token 0em unit, which makes no difference to the calculation, but forces the full expression to come through.

Div 3 is my favourite because it's a little bit self-documenting. I do this because I'm a bit forgetful and it lets me see why I set that element to 728.3 high, not just that I did.


Yes, calc() will work when setting styles in javascript.

Working Example:

var innerDiv = document.getElementsByClassName('inner-div')[0];

function growInnerDiv() {
    innerDiv.style.setProperty('width', 'calc(100% + 224px)');
}

innerDiv.addEventListener('click', growInnerDiv, false);
.outer-div {
    display: inline-block;
    width: 200px;
    height: 100px;
    padding: 12px;
    border: 1px solid rgb(255,0,0);
    background-color: rgb(255,255,0);
}

.inner-div {
    width: 100px;
    height: 100px;
    color: rgb(255, 255, 255);
    font-weight: bold;
    text-align: center;
    line-height: 100px;
    font-family: arial, helvetica, sans-serif;
    background-color: rgb(255,0,0);
    cursor: pointer;
    transition: all 0.5s linear;
}
<div class="outer-div">
    <div class="inner-div">Click Me</div>
<div>