[removed] convert ex to px

后端 未结 2 1126
面向向阳花
面向向阳花 2021-01-22 06:42

So I\'m reading elem.style.width, and getting something like \"3.1415926ex\".

I would like to convert this to \"px.\"

Does JavaScript have built in functions to

2条回答
  •  旧时难觅i
    2021-01-22 06:52

    There is no "magic constant" because there is no straight-forward conversion from ex to px (unlike, say, converting inches to centimetres).

    An ex unit is the height of the lowercase 'x' character in the current font (where a "font" is a combination of typeface family (e.g. Times New Roman), size (say, 14px or 72pt), and style (regular, bold, italic)).

    ...so "1ex" in 72pt Impact Bold is larger than "1ex" in 8pt Helvetica Regular.

    Converting from ex to px can be done if you know these details. Of course, it sounds like you really just want the "computed style", which is always in pixels. Read this: How do I get a computed style?

提交回复
热议问题