当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML Style textOverflow用法及代码示例


HTML DOM中的Style textOverflow属性用于指定文本在包含元素框溢出时的行为。

用法:

  • 它返回textOverflow属性。
    object.style.textOverflow
  • 它用于设置textOverflow属性。
    object.style.textOverflow = "clip|ellipsis|initial|inherit"

属性值:


  • clip:如果内容溢出,则用于剪辑内容。它是默认值。

    例:

    <!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>                    

    输出:
    在单击按钮之前:
    clip-before
    单击按钮后:
    clip-after

  • ellipsis:当文本溢出时,它用于显示省略号。

    例:

    <!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>                    

    输出:
    在单击按钮之前:
    ellipsis-before
    单击按钮后:
    ellipsis-after

  • initial:它用于将此属性设置为其默认值。

    例:

    <!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>                    

    输出:
    在单击按钮之前:
    initial-before
    单击按钮后:
    initial-after

  • inherit:它从其父元素继承属性。
    Example:
    <!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>                    

    输出:
    在单击按钮之前:
    inherit-before
    单击按钮后:
    inherit-after

支持的浏览器:下面列出了textOverflow属性支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • 苹果Safari
  • Opera


相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 HTML | DOM Style textOverflow Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。