Is it possible to get the width of the window in em units using javascript?

前端 未结 5 1798
误落风尘
误落风尘 2021-01-31 14:55

I\'m looking for a reliable way to get the width of the window in em units using javascript. I was surprised to see that jQuery will only return a result in pixel measurements.

5条回答
  •  礼貌的吻别
    2021-01-31 15:09

    Here's a solution that doesn't require jQuery, and doesn't require an explicit font-size declaration.

    window.innerWidth / parseFloat(
      getComputedStyle(
        document.querySelector('body')
      )['font-size']
    )
    

提交回复
热议问题