How can i get default font size in pixels by using JavaScript or JQuery?

前端 未结 9 1449
Happy的楠姐
Happy的楠姐 2021-02-04 09:07

As you know em is a relative font measurement where one em is equal to the height of the letter \"M\" in the default font size. An advantage in using it is because you will be a

9条回答
  •  北海茫月
    2021-02-04 09:39

    There are a couple of situations this can be useful-

    function getDefaultFontSize(pa){
     pa= pa || document.body;
     var who= document.createElement('div');
    
     who.style.cssText='display:inline-block; padding:0; line-height:1; position:absolute; visibility:hidden; font-size:1em';
    
     who.appendChild(document.createTextNode('M'));
     pa.appendChild(who);
     var fs= [who.offsetWidth, who.offsetHeight];
     pa.removeChild(who);
     return fs;
    }
    

    alert(getDefaultFontSize())

提交回复
热议问题