HTML DOM Style fontSize 屬性
定義和用法
fontSize 屬性設置或返回文本的字體尺寸。
語法
設置fontSize 屬性:
Object.style.fontSize="value|inherit"
返回fontSize 屬性:
Object.style.fontSize
值 | 描述 |
---|---|
xx-small x-small small medium large x-large xx-large |
把字体的尺寸设置为不同的固定尺寸,从 xx-small 到 xx-large。 |
smaller | 把字体尺寸减小一个相对单位。 |
larger | 把字体尺寸增大一个相对单位。 |
length | 使用 px、cm 等单位定义字体尺寸。 |
% | 定义字体尺寸为父元素字体尺寸的某个百分比。 |
inherit | fontSize 属性的值从父元素继承。 |
瀏覽器支持
所有主要瀏覽器都支持fontSize 屬性。
注意: IE7及更早的版本不支持"inherit"值。IE8 只有規定了!DOCTYPE 才支持"inherit"。 IE9 支持"inherit"。
實例
實例
更改文本的字體尺寸:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.fontSize="xx-large";
}
</script>
</head>
<body>
<p id="p1">這是一些文本。 </p>
<br>
<button type="button" onclick="displayResult()">修改字體大小</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.fontSize="xx-large";
}
</script>
</head>
<body>
<p id="p1">這是一些文本。 </p>
<br>
<button type="button" onclick="displayResult()">修改字體大小</button>
</body>
</html>
嘗試一下»
Style對象