I am trying to get the value of an inherited CSS property using Javascript. I could not find a comprehensive answer.
CSS example:
div {
width: 80%;
}
Layout Example:
<div id="mydiv"> Some text </div>
Using javascript (jQuery or native), I need to get the width of the element - not in pixels, but in the string "80%".
$('#mydiv').css('width');
$('#mydiv')[0].style.width
getComputedStyle($('#mydiv')[0]).width
The reason I need the value as a string is because I need to copy the style to another element. If it is declared as a percentage, the other value must be a percentage. If it is declared in px, the other value should be in px.
The real trick is that this property can be inherited rather than explicitly point to an element (as in my example).
Does anyone have any ideas?
source
share