Css: Truncating Long Text With "..."

February 8, 2024

Photo of Turned on Laptop Computer
Photo by Danny Meneses on Pexels.

CSS: Truncating Long Text With "..."

Do you ever find yourself in a situation where you have a long piece of text on your website that just doesn't fit within its container? Maybe it's a product description, a blog post, or a user comment. Whatever the case may be, it's important to ensure that your website's text remains visually appealing and easy to read. One solution to this problem is truncating long text with an ellipsis (...).

Why truncate long text?

Before we dive into the details of how to truncate long text with CSS, let's talk about why you might want to do it in the first place. There are a few reasons why you might want to truncate long text:

How to truncate long text with CSS

There are a few different ways to truncate long text with CSS. In this article, we'll cover three common methods: using the text-overflow property, using the overflow property with a fixed height, and using the clamp function.

Method 1: Using text-overflow

The text-overflow property in CSS allows you to specify what should happen when text overflows its container. By setting it to ellipsis, you can indicate that the text should be truncated with an ellipsis. Here's an example:

.container {
  width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

In this example, any text that overflows the 300px width of the container will be truncated with an ellipsis. The white-space: nowrap; property ensures that the text doesn't wrap to a new line.

Method 2: Using overflow with a fixed height

Another way to truncate long text is by setting a fixed height on the container and using the overflow property to hide any content that overflows. Here's an example:

.container {
  height: 100px;
  overflow: hidden;
}
.container p {
  margin: 0;
}

In this example, any text that exceeds the 100px height of the container will be hidden. The margin: 0; is used to remove any vertical spacing between paragraphs.

Method 3: Using the clamp function

The clamp function is a relatively new CSS feature that allows you to specify a range of acceptable values for a property. You can use it to truncate text by setting a minimum font size, a maximum font size, and a preferred line height. Here's an example:

.container {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

In this example, any text that exceeds the height of three lines will be truncated. The preferred line height and font size will be automatically adjusted to fit the available space.

Conclusion

Truncating long text with an ellipsis is a useful technique for ensuring that your website remains visually appealing and easy to read. By using CSS properties like text-overflow, overflow, and the clamp function, you can easily truncate text to fit within its container. Experiment with these methods and find the one that works best for your specific needs.