HTML - DOM樣式物件font屬性



HTML DOM 樣式物件**font**屬性設定或返回字型屬性。它是一個簡寫屬性,包含下面列出的六個不同的屬性。在六個屬性中,font-size和font-family是必需的屬性值。

  • font-style
  • font-variant
  • font-weight
  • font-size
  • line-height
  • font-family

語法

設定 font 屬性
object.style.font= "font-style | font-variant | font-weight | font-size | line-height | caption | icon | menu | message-box | small-caption | status-bar | initial | inherit;";
獲取 font 屬性
object.style.font;

屬性值

描述
font-style 指定元素的字型樣式。
font-variant 指定元素的小寫字母字型。
font-weight 指定元素的字型粗細。
font-size 指定元素的字型大小。
line-height 指定行間距。
font-family 指定元素的字型。
caption 指定用於標題控制元件(例如按鈕)的字型。
icon 指定用於標記圖示的字型。
menu 指定選單中使用的字型。
message-box 指定對話方塊中使用的字型。
small-caption 指定小型控制元件中使用的字型。
status-bar 指定視窗狀態列中使用的字型。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的不同字型屬性。

HTML DOM 樣式物件“font”屬性示例

以下示例將不同的字型屬性應用於段落元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object font Property
    </title>
</head>
<body>
    <p>Click to change different font properties.</p>
    <button onclick="fun()">Change Font</button>
    <p id="font">Welcome to Tutorials Point.</p>
    <script>
        function fun() {
            document.getElementById("font").style.font=
                "oblique bold 20px larger Brush Script MT, cursive";
        }
    </script>
</body>
</html>

支援的瀏覽器

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