HTML - DOM Style 物件 minWidth 屬性



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

語法

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

屬性值

描述
長度 它以長度單位指定元素的最小寬度。
百分比 它以父元素的百分比指定最小寬度
初始 用於將此屬性設定為其預設值。
繼承 用於繼承其父元素的屬性。

返回值

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

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

以下示例說明了如何設定和獲取 div 元素的最小寬度。

設定 div 元素的最小寬度

以下示例將 div 元素的最小寬度設定為 700px。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object minWidth Property
    </title>
    <style>
        #margin {
            border: 2px solid #04af2f;
            overflow: auto;
            width: 400px;
        }
    </style>
</head>
<body>
    <h3>
        Click to set minimum width.
    </h3>
    <button onclick="fun()">Set min width</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.minWidth = "700px";
        }
    </script>
</body>
</html>

獲取 div 元素的最小寬度

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

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object minWidth Property
    </title>
    <style>
        #margin {
            border: 2px solid #04af2f;
            overflow: auto;
            width: 400px;
        }
    </style>
</head>
<body>
    <h3>
        Click to set minimum width.
    </h3>
    <button onclick="fun()">Set min width</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.minWidth = "700px";
            document.getElementById("width")
                .innerHTML = "Min Width :" + x;
        }
    </script>
</body>
</html>

支援的瀏覽器

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