HTML - DOM Style 物件 fontWeight 屬性



HTML DOM Style 物件的 **fontWeight** 屬性設定或返回字型粗細屬性,該屬性指定單詞中字元的粗細。

語法

設定 fontWeight 屬性
object.style.fontWeight= "normal | lighter | bold | bolder | value | initial | inherit";
獲取 fontWeight 屬性
object.style.fontWeight;

屬性值

描述
normal 這是預設值,指定普通字型。
lighter 指定比普通字型更細的字型。
bold 指定粗體字型。
bolder 指定更粗的字型。
value 它接受 100-900 之間的值,其中 400 指定普通值,700 與粗體相同。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示字型的粗細。

HTML DOM Style 物件“fontWeight”屬性示例

以下示例將不同的字型粗細值設定為 p 元素中的文字。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object fontWeight Property
    </title>
</head>
<body>
    <p>Click to set different font weight value.</p>
    <button onclick="fun()">Lighter</button>
    <button onclick="funTwo()">Bold</button>
    <button onclick="funThree()">Bolder</button>
    <button onclick="funFour()">Click</button>
    <p id="font">Welcome to Tutorials Point.</p>    
    <script>
        function fun() {
            document.getElementById("font")
                .style.fontWeight= "lighter";
        }
        function funTwo() {
            document.getElementById("font")
                .style.fontWeight= "bold";
        }
        function funThree() {
            document.getElementById("font")
                .style.fontWeight= "bolder";
        }
        function funFour() {
            document.getElementById("font")
                .style.fontWeight= "300";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
fontWeight 是 2 是 12 是 1 是 1 是 3.5
html_dom_style_object_reference.htm
廣告