HTML - DOM樣式物件marginBottom屬性



HTML DOM 樣式物件 **marginBottom** 屬性設定或返回元素的下邊距。

語法

設定 marginBottom 屬性
object.style.marginBottom= "percent | length | auto | initial | inherit";
獲取 marginBottom 屬性
object.style.marginBottom;

屬性值

描述
百分比 它指定下邊距為父元素寬度的百分比。
長度 它指定下邊距為長度單位,預設值為 0。
auto 它允許瀏覽器設定下邊距值。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的下邊距。

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

以下示例設定並獲取 div 元素的下邊距。

設定下邊距

以下示例使用長度值和百分比設定下邊距。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object marginBottom Property
    </title>
    <style>
        div {
            border: 2px solid #04af2f;
        }
        #margin {
            border: 1px solid black;
            margin: 15px;
        }
    </style>
</head>
<body>
    <h3>
        Click to set bottom margin.
    </h3>
    <button onclick="fun()">Set Margin</button>
    <button onclick="funTwo()">
        Set Margin in Percentage
    </button>
    <br>
    <br>
    <div>
        <div id="margin">
            <p>Welcome to Tutorials Point.</p>
        </div>
    </div>
    <script>
        function fun() {
            document.getElementById("margin")
                .style.marginBottom = "40px";
        }
        function funTwo() {
            document.getElementById("margin")
                .style.marginBottom = "10%";
        }
    </script>
</body>
</html>

獲取下邊距值

以下示例獲取應用於 div 元素的下邊距值。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object marginBottom Property
    </title>
    <style>
        div {
            border: 2px solid #04af2f;
        }
        #margin {
            border: 1px solid black;
            margin: 15px;
        }
    </style>
</head>
<body>
    <h3>
        Click to set bottom margin.
    </h3>
    <button onclick="fun()">Get Margin</button>
    <button onclick="funTwo()">
        Get Margin in Percentage
    </button>
    <br>
    <br>
    <p id="result"></p>
    <br><br>
    <div>
        <div id="margin">
            <p>Welcome to Tutorials Point.</p>
        </div>
    </div>
    <script>
        function fun() {
            let x=document.getElementById("margin")
                .style.marginBottom = "40px";
            document.getElementById("result")
                .innerHTML="Bottom Margin: " +x;
        }
        function funTwo() {
            let x =document.getElementById("margin")
                .style.marginBottom = "10%";
            document.getElementById("result")
                .innerHTML="Bottom Margin: " +x;
        }
    </script>
</body>
</html>

支援的瀏覽器

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