How to add ellipsis in the text using CSS

Learn how to add ellipsis in the text after 1 or any number of lines of text using CSS for better content readability and design. This guide covers simple CSS techniques to handle text overflow efficiently.

How to Add Ellipsis After 1 Line Using CSS

HTML

<div class="ellipsis-one-line">
  This is an example of a single line ellipsis effect using CSS.
</div>

CSS

.ellipsis-one-line {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Browser Support

  • The single-line ellipsis method is widely supported across all modern browsers.

How to Add Ellipsis After Multiple Lines Using CSS

HTML

<div class="ellipsis-multi-lines">
  This is an example of a three-line ellipsis effect using CSS. In this example, the text will be truncated after three lines with an ellipsis at the end to indicate that there is more content available. This technique is useful for managing overflow text in multi-line containers, helping to maintain a clean and organized layout on your website. By applying these CSS properties, you can enhance readability while ensuring that excess text does not disrupt the visual flow of your design. This approach is especially useful for articles, blog posts, and content-heavy pages where space is a consideration.
</div>

CSS

.ellipsis-multi-lines {
  display: -webkit-box;
  -webkit-line-clamp: 3; /* Specifies the number of lines to show */
  -webkit-box-orient: vertical; /* Sets the orientation to vertical */
  overflow: hidden;
  text-overflow: ellipsis;
}

Browser Support

  • The multi-line ellipsis method using -webkit-line-clamp is primarily supported by WebKit-based browsers like Chrome, Safari, and the newer versions of Edge.

Explanation

white-space: nowrapPrevents text from wrapping to the next line.
overflow: hiddenEnsures the overflow text is hidden.
text-overflow: ellipsisAdds the ellipsis at the end of the text
-webkit-line-clamp: 2Specifies the number of lines to show
-webkit-box-orient: verticalSets the orientation to vertical

Resources

minhajulalam.cse@gmail.com

With over four years of experience in software engineering, I’m Minhajul Alam, a passionate coder who thrives on both technical challenges and product management. My journey in software development has been driven by a deep love for coding and a continual desire to learn and grow. I am dedicated to crafting innovative solutions and improving user experiences, always staying curious about the latest technologies and methodologies. My commitment to excellence in both coding and product management allows me to contribute effectively to the creation of impactful and user-centric software products.

Leave a Reply

Your email address will not be published. Required fields are marked *