HTML - DOM Style 物件 backgroundClip 屬性



HTML DOM Style 物件的 **backgroundClip** 屬性設定或返回背景的繪製區域。

語法

以下是獲取或設定 backgroundClip 屬性的語法。

設定 backgroundClip 屬性
object.style.backgroundClip= "border-box | padding-box | content-box | initial | inherit" ;
獲取 backgroundClip 屬性
object.style.backgroundClip;

屬性值

描述
border-box 這是預設值,將背景剪裁到盒子的邊框。
padding-box 它將背景剪裁到盒子的填充。
content-box 它將背景剪裁到盒子的內容。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的 background-clip 屬性。

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

以下示例說明了 backgroundClip 屬性,以使用不同的屬性值指定繪製區域。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object backgroundClip Property
    </title>
    <style>
        #clip {
            width: 400px;
            height: 300px;
            padding: 150px;
            background-color: #04af2f;
            background-clip: border-box;
            color: white;
            border: 3px dotted black;
        }
    </style>
</head>
<body>
    <p>
        Click to try different background clipping.
    </p>
    <button onclick="content()">Content Clip</button>
    <button onclick="border()">Border Clip</button>
    <button onclick="padding()">Padding Clip</button>
    <div id="clip">Here is some random text to bore you...</div>
    <script>
        function content() {
            document.getElementById("clip")
            .style.backgroundClip = "content-box";
        }
        function border() {
            document.getElementById("clip")
            .style.backgroundClip = "border-box";
        }
        function padding() {
            document.getElementById("clip")
            .style.backgroundClip = "padding-box";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
backgroundClip 是 1 是 12 是 4 是 5 是 10.5
html_dom_style_object_reference.htm
廣告