CSS text-overflow

The CSS text-overflow property is used to define, how the hidden overflowed content, should appear to the user. For example:

HTML with CSS Code
<!DOCTYPE html>
<html>
<head>
   <style>
      p{width: 160px; white-space: nowrap; overflow: hidden;
        text-overflow: ellipsis;}
   </style>
</head>
<body>
   
   <h2>The text-overflow Property</h2>
   <p>This is an example of text-overflow property in CSS.</p>
   
</body>
</html>
Output

The text-overflow Property

This is an example of text-overflow property in CSS.

In above example, because the width of p (paragraph) is set to 160px, then anything (text) wider than 160px, gets overflowed.

Note - We need to define the white-space with nowrap, and overflow with hidden, to make the text-overflow property to work.

CSS text-overflow Syntax

The syntax of text-overflow property in CSS, is:

text-overflow: x;

The value of x should be any of the following:

CSS text-overflow Example

HTML with CSS Code
<!DOCTYPE html>
<html>
<head>
   <style>
      p{width: 165px; white-space: nowrap; overflow: hidden; border: 2px solid coral;}
      .a p{text-overflow: ellipsis;}
      .b p{text-overflow: clip;}
      .c p{text-overflow: "-";}
   </style>
</head>
<body>
   
   <div class="a">
      <h2>text-overflow: ellipsis</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
   </div>

   <div class="b">
      <h2>text-overflow: clip</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
   </div>

   <div class="c">
      <h2>text-overflow: "-"</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
   </div>
   
</body>
</html>
Output

text-overflow: ellipsis

Lorem ipsum dolor sit amet consectetur adipisicing elit.

text-overflow: clip

Lorem ipsum dolor sit amet consectetur adipisicing elit.

text-overflow: "-"

Lorem ipsum dolor sit amet consectetur adipisicing elit.

Note - Most of the time, ellipsis is used to define the text-overflow property to signal the hidden overflowed content to the user.

CSS Online Test


« Previous Tutorial Next Tutorial »