0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【HTML】フォントの高さを取得する【JavaScript】

Last updated at Posted at 2020-01-30

フォントの高さを取得する関数です。
measureTextHeight("24px メイリオ")のようにお使いください。指定フォントの高さをpx単位で返します。

"24px メイリオ"を渡すと36を返します。
"1em メイリオ"を渡すと24を返します。

function measureTextHeight(font) {
    
    var div = document.createElement('div');
    div.innerHTML = 'M';
    div.style.margin = '0 !important';
    div.style.padding = '0 !important';
    div.style.position = 'absolute !important';
    div.style.left = '-99999px !important';
    div.style.font = font;
    document.body.appendChild(div);
    height = div.offsetHeight;
    document.body.removeChild(div);
    
    return height;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?