0

I compare different CSS values of the same CSS declaration applied to the same element from different style sheets and to get the final CSS value I use getComputedStyle(). I know that it returns computed values in px. But is it possible at that to get the real value in percents from the very CSS?

I mean with the code

let element = document.querySelector('.col');
console.log(window.getComputedStyle(element).width);
.col{
width:40%;
}
.col{
width:50%;
}
<div class="col"></div>

I get a value in px, but I would like to get 50% specfied in the very CSS

Is there a way?

stckvrw
  • 1,689
  • 18
  • 42
  • Does this answer your question? [How to get CSS class property in Javascript?](https://stackoverflow.com/questions/20377835/how-to-get-css-class-property-in-javascript) – pilchard Sep 06 '21 at 09:07
  • @DBS I know about `styleSheets.cssRules` and they return values in percents, but they don't know which value is the final. The `getComputedStyle()` returns only the final value but computed in px and not in percents, that's the problem. – stckvrw Sep 06 '21 at 09:18
  • What exactly are you trying to achieve with the percentage value? If you know the style is a percentage, you could always work backwards with the pixel value and the parent size to work it out. – DBS Sep 06 '21 at 09:25
  • Ok, thanks, I've got it. – stckvrw Sep 06 '21 at 09:35

0 Answers0