HTML - DOM样式对象overflow属性



HTML DOM 样式对象**overflow**属性决定如何处理不适合元素框的内容。

语法

设置 overflow 属性
object.style.overflow= "visible | hidden | scroll | auto | initial | inherit";
获取 overflow 属性
object.style.overflow;

属性值

描述
visible 这是默认值,其中元素框外的内容不会被裁剪,而是显示在元素框之外。
hidden 它裁剪元素框外的内容,只显示元素框内的内容,其余内容隐藏。
scroll 它裁剪内容以适应元素框,并为元素框外的内容添加滚动条。
auto 它裁剪内容,仅在需要时且内容溢出时添加滚动条。
initial 用于将此属性设置为其默认值。
inherit 用于继承其父元素的属性。

返回值

它返回一个字符串值,表示在元素框之外呈现的内容。

HTML DOM 样式对象“overflow”属性示例

以下示例说明了用于隐藏和向 div 元素添加滚动的 overflow 属性。

隐藏溢出的内容

以下示例使用**hidden**属性值隐藏元素框外的溢出内容。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflow Property
    </title>
    <style>
        #overflow {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="funtwo()">Hide</button>
    <br><br><br>
    <div id="overflow">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            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.
        </p>
        <p>
            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 funtwo() {
            document.getElementById("overflow")
                .style.overflow = "hidden";
        }
    </script>
</body>
</html>

向 div 元素添加滚动条

以下示例使用**scroll**属性值向 div 元素添加滚动条。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflow Property
    </title>
    <style>
        #overflow {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Add Scrollbar</button>
    <br><br><br>
    <div id="overflow">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            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.
        </p>
        <p>
            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("overflow")
                .style.overflow = "scroll";
        }
    </script>
</body>
</html>

将 overflow 属性设置为 auto

以下属性将 overflow 属性设置为**auto**,这会在 div 元素的右侧添加一个滚动条。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflow Property
    </title>
    <style>
        #overflow {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Auto</button>
    <br><br><br>
    <div id="overflow">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            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.
        </p>
        <p>
            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("overflow")
                .style.overflow = "auto";
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
overflow 是 1 是 12 是 1 是 1 是 7
html_dom_style_object_reference.htm
广告