How to truncate long string with ellipsis using CSS

Topic: HTML / CSS« Prev|Next »

Answer: Use the CSS text-overflow property

You can use the CSS text-overflow property in combination with the white-space and overflow properties to truncate or clip long text and put the dot-dot or ellipsis (...) at the end to represent the clipped text or indicate the user.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>Truncating Long Text with CSS</title>
  • <style type="text/css">
  •     p {
  •         width: 400px;
  •         padding: 10px;
  •         background: #f2f2f2;
  •         overflow: hidden;
  •         white-space: nowrap;        
  •         text-overflow: ellipsis;
  •     }
  • </style>
  • </head>
  • <body>
  •     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit ame sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante.</p>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:


Your Feedback:


We would love to hear from you! Please say something.