Home > Mobile >  Getting the padding in pixels, from an element with the value originally assigned as percentage
Getting the padding in pixels, from an element with the value originally assigned as percentage

Time:03-27

I have a div with left padding assigned with percentage, as follows:

padding-left: 1%;

And after resizing the window, I need to get the value in pixels, to make some computations. Using Javascript element.style.paddingLeft I get the original String "1%". ¿How can I get the real padding assigned in percentage but in pixels?

I can't edit the main CSS file, so changing the units from % to px is not an option.

Thanks in advance.

CodePudding user response:

You can do some maths since you can get the width of the window by using window.innerWidth.

let yourPadding = (document.getElementById("yourDiv")element.style.paddingLeft * window.innerWidth)/100;
  • Related