HTML - DOM Style 物件 maxWidth 屬性



HTML DOM Style 物件的 **maxWidth** 屬性用於設定或返回元素的最大寬度。它僅影響塊級元素或具有固定或絕對定位的元素。

語法

設定 maxWidth 屬性
object.style.maxWidth= "none | length | percentage | initial | inherit";
獲取 maxWidth 屬性
object.style.maxWidth;

屬性值

描述
none 這是預設值,它不對最大寬度設定任何限制。
長度 它以長度單位指定元素的最大寬度。
百分比 它以父元素的百分比指定最大寬度
initial 它用於將此屬性設定為其預設值。
inherit 它用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的最大寬度。

HTML DOM Style 物件“maxWidth”屬性的示例

以下示例說明了如何設定 div 元素的最大寬度。

設定 div 元素的最大寬度

以下示例使用 **長度** 和 **百分比** 屬性值設定 div 元素的最大寬度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object maxWidth Property
    </title>
</head>
<body>
    <h3>
        Click to set maximum width.
    </h3>
    <button onclick="fun()">Set Max width</button>
    <button onclick="funTwo()">
        Set Max width in Percentage
    </button>
    <br>
    <div id="margin">
        <p>
            CSS is the acronym for "Cascading 
            Style Sheet". It's a style sheet 
            language used for describing the 
            presentation of a document written 
            in a markup language like HTML. 
        </p>
        <p>
            CSS helps the web developers to 
            control the layout and other visual 
            aspects of the web pages. CSS plays 
            a crucial role in modern web development 
            by providing the tools necessary to 
            create visually appealing, accessible,
            and responsive websites.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("margin")
                .style.maxWidth = "150px";
        }
        function funTwo() {
            document.getElementById("margin")
                .style.maxWidth = "40%";
        }
    </script>
</body>
</html>

獲取 div 元素的最大寬度

以下示例獲取 div 元素的最大寬度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object maxWidth Property
    </title>
</head>
<body>
    <h3>
        Click to set maximum width.
    </h3>
    <button onclick="fun()">Get Max width</button>
    <button onclick="funTwo()">
        Set Max width in Percentage
    </button>
    <br>
    <p id="width"></p>
    <div id="margin">
        <p>
            CSS is the acronym for "Cascading
            Style Sheet". It's a style sheet
            language used for describing the
            presentation of a document written
            in a markup language like HTML.
        </p>
        <p>
            CSS helps the web developers to
            control the layout and other visual
            aspects of the web pages. CSS plays
            a crucial role in modern web development
            by providing the tools necessary to
            create visually appealing, accessible,
            and responsive websites.
        </p>
    </div>
    <script>
        function fun() {
            let x = document.getElementById("margin")
                .style.maxWidth = "150px";
            document.getElementById("width")
                .innerHTML = "Max Width :" + x;
        }
        function funTwo() {
            let x = document.getElementById("margin")
                .style.maxWidth = "40%";
            document.getElementById("width")
                .innerHTML = "Max Width :" + x;
        }
    </script>
</body>
</html>

支援的瀏覽器

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