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
广告