Style font 属性
Style 对象的 font 属性以速记形式设置或返回最多六个独立的字体属性
通过该属性,我们可以设置/返回
- 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 html> 才支持 "inherit"
IE9 支持 "inherit"
更改文本的字体
<script> function displayResult(){ document.getElementById("p1").style.font="italic bold 20px arial,serif"; } </script> <p id="p1">这是一些文本</p> <br> <button type="button" onclick="displayResult()">修改字体</button>