【JavaScript】html要素の基準フォントサイズを取得する
2247 ワード
html要素の基準フォントサイズ取得
WEBサイトのレスポンシブル対応などで、html要素の基準フォントサイズが知りたくなったときにそれを取得することがあったのでメモ。
let root = document.documentElement; //htmlのルート要素を取得
let style = window.getComputedStyle(root).getPropertyValue('font-size'); //ルート要素のcssプロパティを全て取得し、その中からフォントサイズを取得
let stFontSize = parseFloat(style); //float型の数値に変換
window.getComputedStyleでcssプロパティを取得
let style = window.getComputedStyle(root).getPropertyValue('font-size');
let style = window.getComputedStyle(root).getPropertyValue('font-size');
この部分がポイントで、 window.getComputedStyle()の引数に入れたelementのcssプロパティを全て返してくれます。
https://developer.mozilla.org/ja/docs/Web/API/Window/getComputedStyle
なので、基準フォントサイズに限らず、引数を変えれば好きなelementのcssプロパティを取得出来ます。
Author And Source
この問題について(【JavaScript】html要素の基準フォントサイズを取得する), 我々は、より多くの情報をここで見つけました https://qiita.com/yamamo_yamayama/items/2e7a60decbe467164750著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .