HTML - DOM樣式物件backgroundOrigin屬性



HTML DOM 樣式物件**backgroundOrigin**屬性設定或返回背景影像相對於填充、邊框和內容的相對位置。

語法

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

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

屬性值

描述
padding-box 這是預設值,它將背景影像相對於填充框的左上角定位。
border-box 它將背景影像相對於邊框框的左上角定位。
content-box 它將背景影像相對於元素的內容框定位。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

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

以下示例說明了 backgroundOrigin 屬性的不同屬性值。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object backgroundClip Property
    </title>
    <style>
        #origin {
            width: 400px;
            height: 300px;
            padding: 150px;
            border: 1px solid black;
            background: url('/html/images/logo.png') no-repeat;
        }
    </style>
</head>
<body>
    <p>
        Click to try different background image positioning..
    </p>
    <button onclick="content()">Content</button>
    <button onclick="padding()">Padding</button>
    <button onclick="border()">Border</button>
    <div id="origin">Here is some random text to bore you...
        HTML DOM Style Object backgroundClip property 
        sets or returns the painting area of the background.
    </div>
    <script>
        function content() {
            document.getElementById("origin")
            .style.backgroundOrigin = "content-box";
        }
        function padding() {
            document.getElementById("origin")
            .style.backgroundOrigin = "padding-box";
        }
        function border() {
            document.getElementById("origin")
            .style.backgroundOrigin = "border-box";
        }
    </script>
</body>
</html>

支援的瀏覽器

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