I am trying to return a style property in all valid units of length and percentage converted from the original value set for this property.
For example, if I have a div with style.width equal to 20%, I would like to return an object with this value in percent (of course, 20%), pixels (regardless of the actual pixel width), em, pt, ex, etc. d.
I understand that “percentage” is not a “length” value, and not all properties that accept length values accept a percentage, but also want to include this.
Of course, some values will depend on the specific element and, possibly, its position in the DOM (for example, to obtain the value of em, the size of the calculated size of the parent element will also be required).
I can assume that the style is explicitly set for the element - I know how to get the current calculated style of the element - I just hope not to repeat the work that someone else has already done. I also know http://www.galasoft.ch/myjavascript/WebControls/css-length.html , but it relies on style.pixelWidth or node.clientWidth and doesn’t work in Chrome (I’d assume that it doesn’t work either in Safari ... and possibly others).
I have already received color values (rgb, rgba, hex, name) - this, of course, is much more straightforward. I work with values that are mathematically mutable, so only the values “length” and “percent” are needed (if called in a set of properties with a length, not a percentage value, for example, “font-size: large” - the function may fail or cause an error )
if they are written procedurally, something like this would be ideal:
function getUnits(target, prop){ var value =
I do not ask someone to write code for me, but I hope that someone has already done this before, and it is available in some open source library, in the framework, in the blog, anyway. otherwise, if someone has a clever idea on how to optimize the process, that would be great (the author of the link above created a temporary div and calculated one value to determine the coefficients for the other units - a convenient idea, but not for sale completely, and definitely one that needs additional logic to handle everything that I hope agrees on).
in advance for any information or suggestions.