Style fontプロパティ
Style オブジェクト
定義と用法
font プロパティは、ショートハンド形式で最大6つの独立したフォントプロパティを設定または返します。
このプロパティを使用して、以下を設定/取得できます:
- font-style
- font-variant
- font-weight
- font-size
- line-height
- font-family
上記のプロパティを個別に設定することもできます。個別のプロパティを使用すると、より制御しやすくなるため、強くお勧めします。
構文
font プロパティを設定する:
Object.style.font="[style variant weight size/lineHeight family]
or [reserved word]"
font プロパティを返す:
Object.style.font
ヒント:font プロパティにはデフォルト値がありません。
| 值 | 説明 |
|---|---|
| style | フォントスタイルを設定します。 |
| variant | テキストをスモールキャピタル(小型大文字)で表示するように設定します。 |
| weight | フォントの太さを設定します。 |
| size | フォントサイズを設定します。 |
| lineHeight | 行間の距離を設定します。 |
| family | フォントファミリーを設定します。 |
| caption | コントロール(ボタン、ドロップダウンリストなど)のフォントを定義します。 |
| icon | アイコンのラベルに使用するフォントを定義します。 |
| menu | メニューで使用するフォントを定義します。 |
| message-box | ダイアログボックスで使用するフォントを定義します。 |
| small-caption | 小型コントロールで使用するフォントを定義します。 |
| status-bar | ウィンドウのステータスバーで使用するフォントを定義します。 |
| inherit | font プロパティの値は親要素から継承されます。 |
ブラウザのサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザが font プロパティをサポートしています。
注意:IE7 以前のバージョンは "inherit" 値をサポートしていません。IE8 は !DOCTYPE が指定されている場合のみ "inherit" をサポートします。IE9 は "inherit" をサポートします。
例
例
テキストのフォントを変更する:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>初心者チュートリアル(example.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.font="italic bold 20px arial,serif";
}
</script>
</head>
<body>
<p id="p1">これはサンプルテキストです。</p>
<br>
<button type="button" onclick="displayResult()">フォントを変更</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>初心者チュートリアル(example.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.font="italic bold 20px arial,serif";
}
</script>
</head>
<body>
<p id="p1">これはサンプルテキストです。</p>
<br>
<button type="button" onclick="displayResult()">フォントを変更</button>
</body>
</html>
試してみる »
Style オブジェクト その他の拡張