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