HTML - DOM Style 物件 outlineWidth 屬性



HTML DOM Style 物件的 **outlineWidth** 屬性設定或返回元素的輪廓寬度。

語法

設定 outlineWidth 屬性
object.style.outlineWidth= "thin | medium | thick | length | initial | inherit";
獲取 outlineWidth 屬性
object.style.outlineWidth;

屬性值

描述
thin 指定細輪廓。
medium 預設值,指定中等輪廓。
thick 指定粗輪廓。
length 以 CSS 測量單位(如 px)指定輪廓寬度。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

返回一個字串值,表示元素的輪廓寬度。

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

以下示例說明如何設定和獲取輪廓寬度值。

設定 div 元素的輪廓寬度

以下示例將 div 元素的輪廓寬度設定為 thin、thick 和 10px。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object outlineWidth Property
    </title>
    <style>
        #outline {
            border: 2px solid yellow;
            outline: medium solid #04af2f;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Thin Outline</button>
    <button onclick="funtwo()">Thick Outline</button>
    <button onclick="funthree()">Set Width</button>
    <br><br><br>
    <div id="outline">
        Welcome to Tutorials Point...
    </div>
    <script>
        function fun() {
            document.getElementById("outline")
                .style.outlineWidth = "thin";
        }
        function funtwo() {
            document.getElementById("outline")
                .style.outlineWidth = "thick";
        }
        function funthree() {
            document.getElementById("outline")
                .style.outlineWidth = "10px";
        }
    </script>
</body>
</html>

獲取 div 元素的輪廓寬度

以下示例獲取 div 元素的輪廓寬度。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object outlineWidth Property
    </title>
    <style>
        #outline {
            border: 2px solid yellow;
            outline: medium solid #04af2f;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Thin Outline</button>
    <button onclick="funtwo()">Thick Outline</button>
    <button onclick="funthree()">Set Width</button>
    <br><br><br>
    <p id="result"></p>
    <div id="outline">
        Welcome to Tutorials Point...
    </div>    
    <script>
        function fun() {
            let x=document.getElementById("outline")
                .style.outlineWidth = "thin";
            document.getElementById("result")
                .innerHTML="Outline Width :" +x;
        }
        function funtwo() {
            let x=document.getElementById("outline")
                .style.outlineWidth = "thick";
            document.getElementById("result")
                .innerHTML="Outline Width :" +x;
        }
        function funthree() {
            let x=document.getElementById("outline")
                .style.outlineWidth = "10px";
            document.getElementById("result")
                .innerHTML="Outline Width :" +x;
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
outlineWidth 是 1 是 12 是 1.5 是 1.2 是 7
html_dom_style_object_reference.htm
廣告