Css text wrap ellipsis

How to handle long text overflow with pure CSS (truncate/ellipsis, wrap)

When working on a website or a web app texts are often overlooked, that’s when issues like overflowing text occur. To solve that, you can use some solutions like truncating or ellipsizing a text (add three dots) or wrapping the text.

Overflowing text content quite often happens in the following cases:

  • For long words;
  • For long URL’s;
  • On mobile devices;
  • For narrow container elements;
  • For table cells;
  • For button elements.

Depending on the CSS styles you have, the text overflow will usually look either like a horizontal scroll or like a cut-off content.

Consider the following issue. There’s a fixed-width container on a page with a link containing and pointing to a long URL. The link text will overflow the container and will look messy, as well as it can produce an unwanted horizontal scroll on smaller screen sizes.

1. Wrapping text

1.2 The word-break property

One way to handle long text in CSS is to wrap it to the next line. This approach is handy when you don’t have to worry about text spanning multiline. When using word-break property you have two options how to wrap it:

  • break-all — this will break text once the characters don’t fit inside the container
  • break-word — this will break text once the characters don’t fit inside the container but it will preserve the word sequence. NOTE: this is a deprecated API and it is not recommended to use.
.container a  word-break: break-all; > 

Browser support for word-break property: https://caniuse.com/word-break

1.2 The overflow-wrap property

Another option to wrap text is to use the overflow-wrap property. This property also has two options for wrapping:

  • anywhere — will break text at any given point if it doesn’t fit;
  • break-word — same as anywhere except it will break long words at arbitrary points.
.container a  overflow-wrap: break-word; > 

Browser support for overflow-wrap property: https://caniuse.com/wordwrap

2. Truncating text

2.1 The text-overflow property

The text-overflow property will add an ellipsis (will add three dots) to a text if it doesn’t fit inside the container. This approach is handy if you want to keep text in a single line. However, with some additional modifications, it can be made for multiline text as well.

The text-overflow property with a value of ellipsis must be set on a parent element, relative to the text. Two additional properties must be specified overflow and white-space .

NOTE: Learn how to handle text overflow for select tag.

Single line

.container  overflow: hidden; white-space: nowrap; text-overflow: ellipsis; > 

Multiline

For the multiline solution the white-space property must be removed, this way the text will be truncated on the last available line.

.container  overflow: hidden; text-overflow: ellipsis; > 

Browser support for text-overflow property: https://caniuse.com/text-overflow

2.2 The line-clamp property

A more modern approach is the line-clamp property which will limit the container text to a specified number of lines. As well as add the three dots at the end. This property will only work with a -webkit- vendor prefix and a display property set to -webkit-box or -webkit-inline-box .

Unline the text-overflow solution, this approach is straightforward and more understandable, when it comes to truncating multiline text. The line-clamp property must be set to the element that is overflowing and the value should be equal to the number of lines to span.

.container a  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; > 

Browser support for the line-clamp property: https://caniuse.com/css-line-clamp

A full demo is available on CodePen:

Источник

text-overflow

The text-overflow CSS property sets how hidden overflow content is signaled to users. It can be clipped, display an ellipsis (‘ … ‘), or display a custom string.

Try it

The text-overflow property doesn’t force an overflow to occur. To make text overflow its container, you have to set other CSS properties: overflow and white-space . For example:

overflow: hidden; white-space: nowrap; 

The text-overflow property only affects content that is overflowing a block container element in its inline progression direction (not text overflowing at the bottom of a box, for example).

Syntax

text-overflow: clip; text-overflow: ellipsis ellipsis; text-overflow: ellipsis " [..]"; /* Global values */ text-overflow: inherit; text-overflow: initial; text-overflow: revert; text-overflow: revert-layer; text-overflow: unset; 

The text-overflow property may be specified using one or two values. If one value is given, it specifies overflow behavior for the end of the line (the right end for left-to-right text, the left end for right-to-left text). If two values are given, the first specifies overflow behavior for the left end of the line, and the second specifies it for the right end of the line.

Values

The default for this property. This keyword value will truncate the text at the limit of the content area, therefore the truncation can happen in the middle of a character. To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: »; .

This keyword value will display an ellipsis ( ‘…’ , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text. The ellipsis is displayed inside the content area, decreasing the amount of text displayed. If there is not enough space to display the ellipsis, it is clipped.

The to be used to represent clipped text. The string is displayed inside the content area, shortening the size of the displayed text. If there is not enough space to display the string itself, it is clipped.

This keyword clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.

This function clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.

Formal definition

Formal syntax

Источник

CSS text-overflow Property

The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (. ), or display a custom string.

Both of the following properties are required for text-overflow:

Default value: clip
Inherited: no
Animatable: no. Read about animatable
Version: CSS3
JavaScript syntax: object.style.textOverflow=»ellipsis» Try it

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -o- specify the first version that worked with a prefix.

CSS Syntax

Property Values

Value Description Demo
clip Default value. The text is clipped and not accessible Demo ❯
ellipsis Render an ellipsis («. «) to represent the clipped text Demo ❯
string Render the given string to represent the clipped text
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

More Examples

Example

Text-overflow with a hover effect (show entire text on hover):

div.a <
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
>

div.a:hover overflow: visible;
>

Источник

CSS Ellipsis for Single-Line and Multi-Line Text

Learn to truncate text and add ellipsis at the end using CSS. For both, single-line and multi-line text.

CSS Ellipsis for Single-Line and Multi-Line Text

So your designer has asked you to add the three dots at the end when the text doesn’t fit, and you have no clue how to do that?

Don’t worry, it’s super simple to do with CSS.

By the way, those three dots at the end are called ellipsis.

Real quick — would you like to master CSS and learn Web Development? If so, be sure to check out the platform I recommend to learn coding online:

Single-line ellipsis

Use text-overflow: ellipsis; to automatically truncate the text when it overflows the container and add the three dots at the end.

The element needs to get resized and the text has to stay in one line for the ellipsis to show up, so here are all 3 CSS lines you need:

Multi-line ellipsis

You will soon find, that text-overflow: ellipsis; doesn’t work when the text wraps to an extra line.

To truncate multi-line text and add an ellipsis at the end of the last line use the experimental -webkit-box box model (don’t worry it’s supported in all major browsers):

Conclusion

It’s trivial to truncate the overflowing text and add an ellipsis at the end of a single line. Adding an ellipsis for multiline text is a bit more tricky, so this post should help you.

You can find the code examples for this article on my GitHub: https://github.com/vincaslt/twitter-code/tree/main/src/ellipsis

Since you’re learning CSS tricks, why not go through my practical CSS guide? It will take only 5 minutes, but it will teach you all of the CSS you need in practice:

Источник

Читайте также:  Make site with python
Оцените статью