Reputation: 3880
When I inspect webpages and want to adjust elements whose font-size value are in percentage or em
, I always have to go through their ancestors to find out on whom those percentage values are based on. Since this scenario happens a lot in my life, I am trying to write some code to make things easier. But the problem is, jQuery's .css() method is only getting computed values, like below.
style
tag?px
. Like .offsetParent(), which can find the nearest ancestor that is 'positioned'.jsfiddle: http://jsfiddle.net/yae8qn70/
HTML:
<div class='a'>
<div class='b'>120% font</div>
</div>
Result:<span class='r'></span>
css:
.a{font-size:16px}
.b{font-size:120%}
Js:
$('.r').text($('.b').css('font-size'))
Upvotes: 0
Views: 78
Reputation: 1
Try
$('.r').text(function (_, text) {
var parent = window.getComputedStyle($('.b').parent()[0], null)
.getPropertyValue("font-size");
var elem = window.getComputedStyle($('.b')[0], null)
.getPropertyValue("font-size");
var res = (Math.ceil((parseInt(elem) / parseInt(parent)) * 100));
return res + "%"
});
jsfiddle http://jsfiddle.net/yae8qn70/1/
Upvotes: 0
Reputation: 14053
No easy method, I'm afraid, but if you're determined, you could do something like the following:
var styleSheetList = document.styleSheets;
.cssRules
Obviously, you'll have to implement the CSS cascade in your code. Seems like an awful lot of work, but it's at least theoretically possible
Upvotes: 1