Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use CSS calc within Javascript?

Can I use the css calc() function when setting positions in JavaScript?

ePopup.style.top = "calc(100px - 1.5em)";
like image 694
NickC Avatar asked Nov 29 '16 16:11

NickC


People also ask

What is Calc () in Javascript?

The calc() function lets you perform basic math operations on values, and it's especially useful when you need to add or subtract a length value from a percentage. This is how it works: div { max-width: calc(80% - 100px) } It returns a length value, so it can be used anywhere you expect a pixel value. You can perform.

What does CALC () do in CSS?

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.

Can I use Calc in padding?

A solution for you would be to use CSS calc, it has good browser support and fixes your issue in quite a simple manner. The only downside here is that it doesn't calculate the padding-top in % but you simply cannot calculate padding-top in % from the height of the element unless you use javascript.

Can I use Calc for margin CSS?

You can use calc() anywhere where you would use numeric values (e.g.: width, max-height, margin-left, …) Can I Use calc? Data on support for the calc feature across the major browsers from caniuse.com.


Video Answer


2 Answers

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>
like image 156
Rounin - Glory to UKRAINE Avatar answered Oct 19 '22 23:10

Rounin - Glory to UKRAINE


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.

like image 45
Ben Avatar answered Oct 20 '22 00:10

Ben