How to Add CSS Text Ellipsis (...)
This article explains how to add a CSS text ellipsis (...) for long text content. We will cover the following topics with examples:
This guide provides clear examples and practical tips to help beginners understand the concepts easily.
How to Prevent Line Breaks for Single-Line Text and Apply an Ellipsis
Single-line text truncation uses the text-overflow property.
By default, text without specific CSS styling automatically wraps to the next line when it reaches the edge of the content box. In this section, we will demonstrate how to prevent line wrapping at the edge of the content box, hide the overflowing text, and display an ellipsis (...) instead.
Understanding Through a Demo
div {
width: 200px;
border: 2px solid red;
/* Required properties to prevent wrapping
and display an ellipsis (...) */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div>
This example demonstrates how to prevent automatic line breaks,
hide overflowing text, and display an ellipsis (...)
when the text reaches the end of the content box.
</div>
The implementation of the above demo utilizes the text-overflow property. Understanding the functionality of this property makes it easier to comprehend how to prevent line breaks for single-line text and display an ellipsis.
Detailed Explanation of How to Use the text-overflow Property
The text-overflow property specifies whether an ellipsis (...) is displayed to signal to the user that single-line text has overflowed the element's box and is hidden from view.
This property can be applied to block container elements where the width or height can be specified.
Let’s take a look at the following demo.
CSS Demo: text-overflow
The implementation of this demo is explained step by step.
- This is a normal text area using a
<div>tag without any styling.
<div>
The text-overflow property specifies
whether an ellipsis (...) is displayed to signal to the user
that single-line text has overflowed
the element's box and is hidden from view.
</div>
- Next, we apply a common style to the
<div>tag, which defines the text container used in this demo.
<div> tag as a text container
div {
width: 200px;
border: 2px solid red;
}
A normal text area, as shown in the rendered output above, automatically wraps text when it reaches the edge of the content box.
- To prevent line wrapping and allow text to overflow the container, we apply
white-space: nowrap.
<div style="white-space: nowrap;">
The text-overflow property specifies
whether an ellipsis (...) is displayed to signal to the user
that single-line text has overflowed
the element's box and is hidden from view.
</div>
The white-space property sets how whitespace characters and line break rules are handled inside an element. When set to nowrap, text no longer wraps to the next line when it reaches the edge of the content box. Instead, it overflows the text container.
- Next, we hide the overflowing text using
overflow: hidden.
<div style="white-space: nowrap; overflow: hidden;">
The text-overflow property specifies
whether an ellipsis (...) is displayed to signal to the user
that single-line text has overflowed
the element's box and is hidden from view.
</div>
Additional Explanation
The overflow property specifies how content is handled when it exceeds the size of its element. When set to hidden, overflowing content is clipped within the padding box of the element.
- Finally, we use the
text-overflowproperty to display an ellipsis for the clipped text.
<div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
The text-overflow property specifies
whether an ellipsis (...) is displayed to signal to the user
that single-line text has overflowed
the element's box and is hidden from view.
</div>
When the value of the text-overflow property is set to ellipsis, an ellipsis (...) is displayed as shown in the rendered output above.
Important Notes
The text-overflow property can only be implemented when the following three conditions are met:
- The
overflowproperty must not be set to a value ofvisible. It must be applied with a value ofhidden,scroll, orauto. - The
white-spaceproperty must be applied with a value ofnowraporpre, which prevents automatic line wrapping when text reaches the edge of the content box. - In addition, these properties and values only work when the element is a block container element where the width or height can be specified.
If these three conditions are not met, the text-overflow property will not be applied to the corresponding element. Naturally, this is because it is a property designed to configure whether to display a visually indicated ellipsis under these conditions.
How to Limit Text to a Specific Number of Lines and Apply an Ellipsis
Limiting text to a specific number of lines and displaying an ellipsis is achieved using the -webkit-line-clamp property.
The following demo demonstrates how the -webkit-line-clamp property is utilized to limit the wrapping text inside a text container to three lines and display an ellipsis (...) when the text exceeds the specified number of lines.
Understanding Through a Demo
Let’s take a look at the following demo.
div {
max-width: 200px;
border: 2px solid red;
/* Required CSS to limit text to specified lines
and display an ellipsis (...) */
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
-webkit-line-clamp: 3; /* Number of lines to display */
}
<div>
The -webkit-line-clamp property restricts a text block to a specified number of lines.
This property functions when the display property of the element is set to -webkit-box
and the -webkit-box-orient property is set to vertical.
If the text exceeds the specified number of lines, an ellipsis (...) is displayed.
When the overflow property of the element is set to a value other than visible,
the text is truncated to the specified number of lines;
otherwise, the text continues to be displayed even after the ellipsis appears.
</div>
You can specify the number of lines to restrict text by setting an integer greater than 0 as the value for the -webkit-line-clamp property.
Understanding the functionality of the -webkit-line-clamp property makes it easier to understand how to limit text to a specific number of lines and display an ellipsis.
Detailed Explanation of How to Use the -webkit-line-clamp Property
The -webkit-line-clamp property restricts
text in an element to a set number of lines,
adding an ellipsis (...) at the end if the content exceeds the limit.
- This property functions when the
displayproperty of the element is set to-webkit-box, - and the
-webkit-box-orientproperty is set tovertical. - If the
overflowproperty of the element is set to a value other thanvisible, the text is truncated to the specified number of lines; otherwise, the text continues to be displayed even after the ellipsis (...) appears.
Important Notes
There are two important considerations when implementing the -webkit-line-clamp property.
The display: -webkit-box and -webkit-box-orient: vertical Properties Are Required
The -webkit-line-clamp property only functions when display: -webkit-box and -webkit-box-orient: vertical are applied. It will not be applied to elements where the values of these two properties are not set.
div {
max-width: 400px;
border: 2px solid red;
overflow: visible;
-webkit-line-clamp: 2;
/* display: -webkit-box and
-webkit-box-orient: vertical are not applied here. */
}
<div>
The -webkit-line-clamp property restricts a text block to a specified number of lines.
This property functions when the display property of the element is set to -webkit-box
and the -webkit-box-orient property is set to vertical.
If the text exceeds the specified number of lines, an ellipsis (...) is displayed.
When the overflow property of the element is set to a value other than visible,
the text is truncated to the specified number of lines;
otherwise, the text continues to be displayed even after the ellipsis appears.
</div>
The -webkit-line-clamp property only functions when display: -webkit-box and -webkit-box-orient: vertical are applied. It will not be applied to elements where the values of these two properties are not set.
Applying a Value Other Than visible to the overflow Property of the Element
If the overflow property of the corresponding element is set to a value other than visible (such as hidden, auto, or scroll), the text is clipped to the specified number of lines. Otherwise, the text continues to be displayed even after the ellipsis (...) appears.
The following is an example where the overflow property of the corresponding element is incorrectly set to visible, causing the text to continue displaying even after the ellipsis (...) appears.
div {
max-width: 400px;
border: 2px solid red;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
/* The text continues to be displayed
even after the ellipsis (...) appears. */
overflow: visible;
}
<div>
The -webkit-line-clamp property restricts a text block to a specified number of lines.
This property functions when the display property of the element is set to -webkit-box
and the -webkit-box-orient property is set to vertical.
If the text exceeds the specified number of lines, an ellipsis (...) is displayed.
When the overflow property of the element is set to a value other than visible,
the text is truncated to the specified number of lines;
otherwise, the text continues to be displayed even after the ellipsis appears.
</div>
The text continues to be displayed even after the ellipsis (...) appears.
Browser Compatibility
| Property |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
text-overflow
|
1 | 12 | 7 | 3 |
-webkit-line-clamp
|
6 | 17 | 68 | 5 |
References
See Also
- Centering Text with CSS – Horizontal and Vertical Alignment Tips
- Centering Images with CSS – Horizontal and Vertical Alignment Tips
- Centering Elements with CSS – Horizontal and Vertical Alignment Tips
- CSS List Marker Styling – Examples, Techniques
- CSS <details> Tag Styling – Examples, Techniques
- CSS Scrollbar Customization