How to get fill or margin value in relative value using jQuery

If I have an element with CSS style like code

td class="bogus" style="padding-left: 1em;" 

how can i use jQuery to get padding-left value as 1em instead of pixels?

 $(".bogus").css("padding-left"); 

This only returns pixels, but I want it to return what is really in the code, in this case the relative value of 1em. How?

Thanks for any guidance.

  • Qu
+6
jquery
source share
1 answer

What you can do is set the size of the base font (the font-size property in the <body> element) to about 62.8% (some say 62.5%):

 <body style="font-size:62.5%;"> 

This makes the base font, or 1.0em, approximately equal to 10px. This is not accurate, and varies from font to font, but, generally speaking, it is quite accurate. By doing this, you can use EM and their pixel equivalents:

1.0em = 10px, 1.1em = 11px, 1.2em = 12px, etc.

This way you can easily convert from pixels to EM by dividing by 10:

 var ems = parseInt($(".bogus").css("padding-left")) / 10; 
+8
source share

All Articles