Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM Style overflow Property

  • Difficulty Level : Basic
  • Last Updated : 01 Aug, 2019

The Style overflow property in HTML DOM is used to specify the behavior of the content when it overflows the element box. The content may be hidden, shown or a scrollbar maybe shown according to the value.

Syntax:

  • It returns the overflow property.
    object.style.overflow
  • It is used to set the overflow property.
    object.style.overflow = "visible|hidden|scroll|auto|initial|
    inherit"

Property Values:

  • visible: The content is not clipped and may overflow out of the containing element.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
                overflow: hidden;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is
            used to specify the behavior of the content
            when it overflows the element box.
        </p>
      
        <div class="content">
            GeeksforGeeks is a computer science portal
            with a huge variety of well written and
            explained computer science and programming
            articles, quizzes and interview questions.
            <br>The portal also has dedicated GATE
            preparation and competitive programming 
            sections.
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to visible -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'visible';
            }
        </script>
    </body>
      
    </html>                    

    Output:



    • Before clicking the button:
      visible-before
    • After clicking the button:
      visible-after
  • hidden: The content is clipped and hidden to fit the element. No scrollbars are provided when using this value.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is
            used to specify the behavior of the content
            when it overflows the element box.
        </p>
      
        <div class="content">
            GeeksforGeeks is a computer science portal
            with a huge variety of well written and
            explained computer science and programming
            articles, quizzes and interview questions.
            <br>The portal also has dedicated GATE
            preparation and competitive programming 
            sections.
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to visible -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'hidden';
            }
        </script>
    </body>
      
    </html>                    

    Output:

    • Before clicking the button:
      hidden-before
    • After clicking the button:
      hidden-after
  • scroll: The content is clipped to fit the element box and scrollbars are provided to help scroll the overflowed content. The scrollbar here is added even if the content is not clipped.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
                overflow: hidden;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is
            used to specify the behavior of the content
            when it overflows the element box.
        </p>
      
        <div class="content">
            GeeksforGeeks is a computer science portal
            with a huge variety of well written and
            explained computer science and programming
            articles, quizzes and interview questions.
            <br>The portal also has dedicated GATE
            preparation and competitive programming 
            sections.
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to visible -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'scroll';
            }
        </script>
    </body>
      
    </html>                    

    Output:

    • Before clicking the button:
      scroll-before
    • After clicking the button:
      scroll-after
  • auto: The behavior of auto depends on the content and scrollbars are added only when the content overflows.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
                overflow: visible;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is
            used to specify the behavior of the content
            when it overflows the element box.
        </p>
      
        <div class="content">
            GeeksforGeeks is a computer science portal
            with a huge variety of well written and
            explained computer science and programming
            articles, quizzes and interview questions.
            <br>The portal also has dedicated GATE
            preparation and competitive programming 
            sections.
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to visible -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'auto';
            }
        </script>
    </body>
      
    </html>                    

    Output:

    • Before clicking the button:
      auto-before
    • After clicking the button:
      auto-after
  • initial: It is used to set this property to its default value.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
                overflow: scroll;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is
            used to specify the behavior of the content
            when it overflows the element box.
        </p>
      
        <div class="content">
            GeeksforGeeks is a computer science portal
            with a huge variety of well written and
            explained computer science and programming
            articles, quizzes and interview questions.
            <br>The portal also has dedicated GATE
            preparation and competitive programming 
            sections.
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to visible -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'initial';
            }
        </script>
    </body>
      
    </html>                    

    Output:

    • Before clicking the button:
      initial-before
    • After clicking the button:
      initial-after
  • inherit: This inherits the property from its parent.

    Example:




    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style overflow Property
        </title>
          
        <style>
            #parent {
                overflow: auto;
            }
      
            .content {
                background-color: lightgreen;
                height: 150px;
                width: 200px;
            }
      
            button {
                margin-top: 60px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style overflow Property</b>
          
        <p>
            The Style overflow property in HTML DOM is used
            to specify the behavior of the content when it
            overflows the element box.
        </p>
          
        <div id="parent">
            <div class="content">
                GeeksforGeeks is a computer science portal with
                a huge variety of well written and explained
                computer science and programming articles, 
                quizzes and interview questions.<br>The portal
                also has dedicated GATE preparation and competitive
                programming sections.
            </div>
        </div>
          
        <button onclick="setOverflow()">
            Change overflow
        </button>
      
        <!-- Script to set overflow to inherit -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.overflow = 'inherit';
            }
        </script>
    </body>
      
    </html>                    

    Output:

  • Before clicking the button:
    inherit-before
  • After clicking the button:
    inherit-after

Supported Browsers: The browser supported by DOM Style overflow property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Apple Safari

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :