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