snippetcssTip
Text truncation, using CSS
Viewed 0 times
usingtruncationcsstext
Problem
Text truncation is a tool that many designs make use of to prevent text from overflowing its container. It can be applied both to single line and multiline text, and it is a great way to ensure that the text fits within the design constraints.
https://codepen.io/chalarangelo/pen/rNEggmY
Single line text can be easily truncated using a couple of CSS fundamentals. First off, you'll need to use
> [!NOTE]
>
https://codepen.io/chalarangelo/pen/rNEggmY
Single line text can be easily truncated using a couple of CSS fundamentals. First off, you'll need to use
white-space: nowrap to prevent the text from wrapping to the next line. Then, you can use overflow: hidden to hide any overflow, and text-overflow: ellipsis to add an ellipsis at the end of the text.> [!NOTE]
>
Solution
.truncate-text {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}Single line text can be easily truncated using a couple of CSS fundamentals. First off, you'll need to use
white-space: nowrap to prevent the text from wrapping to the next line. Then, you can use overflow: hidden to hide any overflow, and text-overflow: ellipsis to add an ellipsis at the end of the text.> [!NOTE]
>
> For this technique to work, a fixed
width must be set on the element or its parent.<baseline-support featureId="line-clamp">
</baseline-support>
Code Snippets
.truncate-text {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}.truncate-text-multiline {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}Context
From 30-seconds-of-code: text-truncation
Revisions (0)
No revisions yet.