3

My element has a CSS rule applied to it giving it left: 0%. When I access that element with JS and use el.style.left, I get an empty string. When I use window.getComputedStyle(el), it gives me a pixel value. How can I access the actual "0%" that the CSS has set it to? I need to be able to read this.

I'm only using left as an example though....same goes for all the other CSS properties on the element. How can I see them?

5
  • 2
    I'm afraid that this information gets lost. el.style only works if the rule is inside the style attribute. The computed value is memorized in a format that is useful for the browser, so it only returns pixels. Maybe you could read the css file. Btw I wait for other solutions Commented May 24, 2018 at 10:58
  • A workaround could be getting the pixel value and compare it with the parent element's width, so you get the percentage. Commented May 24, 2018 at 11:03
  • Clever idea! Might try that. Commented May 24, 2018 at 11:04
  • 2
    See stackoverflow.com/questions/744319/… Commented May 24, 2018 at 11:04
  • That would make this a duplicate, I do believe. Commented May 24, 2018 at 11:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.