HTML | DOM Style textOverflow Property

The Style textOverflow property in HTML DOM is used to specify the behavior of the text when it overflows the containing element box.

Syntax:

  • It returns the textOverflow property.
    object.style.textOverflow
  • It is used to set the textOverflow property.
    object.style.textOverflow = "clip|ellipsis|initial|inherit"

Property Values:

  • clip: It is used to clip the content if it overflows. It is the default value.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style textOverflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 100px;
                width: 300px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style textOverflow Property</b>
          
        <p>
            The textOverflow property specifies how text
            should be displayed when it overflows the 
            container element.
        </p>
          
        <div class="content">
            GeeksforGeeks is a computer science portal with
            a huge variety of well written <br>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 textOverflow
        </button>
      
        <!-- Script to set textOverflow to clip -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.textOverflow = 'clip';
            }
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:
    Before clicking the button:
    clip-before
    After clicking the button:
    clip-after

  • ellipsis: It is used to shows ellipsis when the text overflows.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style textOverflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 100px;
                width: 300px;
                white-space: nowrap;
                overflow: hidden;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style textOverflow Property</b>
          
        <p>
            The textOverflow property specifies how text
            should be displayed when it overflows the 
            container element.
        </p>
          
        <div class="content">
            GeeksforGeeks is a computer science portal with
            a huge variety of well written <br>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 textOverflow
        </button>
      
        <!-- Script to set textOverflow to clip -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.textOverflow = 'ellipsis';
            }
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:
    Before clicking the button:
    ellipsis-before
    After clicking the button:
    ellipsis-after

  • initial: It is used to set this property to its default value.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style textOverflow Property
        </title>
          
        <style>
            .content {
                background-color: lightgreen;
                height: 100px;
                width: 300px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style textOverflow Property</b>
          
        <p>
            The textOverflow property specifies how text
            should be displayed when it overflows the 
            container element.
        </p>
          
        <div class="content">
            GeeksforGeeks is a computer science portal with
            a huge variety of well written <br>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 textOverflow
        </button>
      
        <!-- Script to set textOverflow to clip -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.textOverflow = 'initial';
            }
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:
    Before clicking the button:
    initial-before
    After clicking the button:
    initial-after

  • inherit: It inherits the property from its parent element.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
          
    <head>
        <title>
            DOM Style textOverflow Property
        </title>
          
        <style>
            #parent {
                text-overflow: ellipsis;
            }
            .content {
                background-color: lightgreen;
                height: 100px;
                width: 300px;
                white-space: nowrap;
                overflow: hidden;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
          
        <b>DOM Style textOverflow Property</b>
          
        <p>
            The textOverflow property specifies how text
            should be displayed when it overflows the
            container element.
        </p>
          
        <div id="parent">
            <div class="content">
                GeeksforGeeks is a computer science portal with
                a huge variety of well written <br>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 textOverflow
        </button>
      
        <!-- Script to set textOverflow to inherit -->
        <script>
            function setOverflow() {
                elem = document.querySelector('.content');
                elem.style.textOverflow = 'inherit';
            }
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

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

Supported Browsers: The browser supported by textOverflow property are listed below:

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


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.