HTML - DOM Style 物件 overflowX 屬性



HTML DOM Style 物件**overflowX**屬性決定如果內容不適合元素框,如何處理內容的左右邊緣。

語法

設定 overflowX 屬性
object.style.overflowX= "visible | hidden | scroll | auto | initial | inherit";
獲取 overflowX 屬性
object.style.overflowX;

屬性值

描述
visible 這是預設值,其中元素框外的內容不會被剪裁,並在元素框外顯示。
hidden 它剪裁元素框外的內容,僅顯示元素框內的內容,其餘內容隱藏。
scroll 它剪裁內容以適合元素框,併為元素框外的內容新增捲軸。
auto 它剪裁內容,並且僅在需要時以及內容溢位時新增捲軸。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的 overflow-x 屬性。

HTML DOM Style 物件“overflowX”屬性示例

以下示例說明了 overflowX 屬性以隱藏和向 div 元素新增捲軸。

隱藏溢位內容

以下示例使用**hidden**屬性值隱藏元素框外的溢位內容。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowX Property
    </title>
    <style>
        #overflowX {
            white-space: nowrap;
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Hide</button>
    <br><br><br>
    <div id="overflowX">
        <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("overflowX")
                .style.overflowX = "hidden";
        }
    </script>
</body>
</html>

向 div 元素新增捲軸

以下示例使用**scroll**屬性值向 div 元素新增捲軸。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowX Property
    </title>
    <style>
        #overflowX {
            white-space: nowrap;
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Add Scrollbar</button>
    <br><br><br>
    <div id="overflowX">
        <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("overflowX")
                .style.overflowX = "scroll";
        }
    </script>
</body>
</html>

將 overflowX 屬性設定為 auto

以下屬性將 overflowX 屬性設定為**auto**,這會在 div 元素底部新增捲軸。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowX Property
    </title>
    <style>
        #overflowX {
            white-space: nowrap;
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Auto</button>
    <br><br><br>
    <div id="overflowX">
        <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("overflowX")
                .style.overflowX = "auto";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
overflowX 是 1 是 12 是 3.5 是 3 是 9.5
html_dom_style_object_reference.htm
廣告