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

前端 未结 9 1458
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:50

    It can be done using this line of code:

    const fontSize = Number(window.getComputedStyle(document.body).getPropertyValue('font-size').match(/\d+/)[0])
    
    1. window.getComputedStyle(document.body) - to get all the styles for body
    2. getPropertyValue('font-size') - to get a string value of font-size, example: (16px)
    3. match(/\d+/)[0]) - to get only a number part, example: (16) - string
    4. Number(...) - to convert number part into a number, example: (16) - number

提交回复
热议问题