|

Strings in text-overflow: A Win for Truncated Text

Chrome 157 implemented text-overflow: following Firefox 9, which was released in, uhhh… 2011. Safari 27.3 Beta has also shipped it, so we can expect it to be Baseline soon.

But what does text-overflow: do, exactly? Well, text-overflow: ellipsis adds an ellipsis (…) to the end of the line box, replacing however many characters it takes up. This is unlike text-overflow: clip (the default value), which adds nothing.

This text is overflowing its container.

div {
/* Size */
inline-size: 170px;

/* Prevent wrapping */
white-space: nowrap;

/* Hide any overflow */
overflow-inline: hidden;

/* Append string */
text-overflow: “—”;
}

CodePen Embed Fallback

First, the text must overflow a block container in its inline direction. That’s why I’ve declared inline-size: 170px in that last example, which, assuming that the writing-mode computes to horizontal-tb, determines the width.

Next, white-space: nowrap prevents text wrapping, causing overflow whenever there’s no room for the text in the inline direction. Yes, this means that text-overflow is for single-line text (for multi-line text, you want line-clamp).

The overflow must be set to anything but visible.

And finally, text-overflow: “—” adds an em dash (—) to the end of the line box while truncating however many characters it takes up.

Note that the string can be blank (text-overflow: “”). This truncates any overflowing characters without replacing them, unlike the default text-overflow: clip, which can leave fractional characters behind.

CodePen Embed Fallback

What should the string be?

The key is making it obvious that the text is truncated. We can, again, use an em dash as they’re are typically for interruptions and abrupt cut-offs of dialogue.

There’s text-overflow: “…”, but this is pretty much the same thing as text-overflow: ellipsis. However, […] can be used to truncate quotes while signaling that it’s an editorial omission or design decision, not the end of the quoted text. If we want it to be clickable, something akin to [read more] (with the square brackets looking like a makeshift button) is a common format. However, the text-overflow string isn’t clickable, and since square brackets already mean something, I don’t recommend this format.

Other common bracketed interpolations include [truncated], [sic], [snip], and [excerpt], so text-overflow: lends itself to more use cases than you might think.

Carets (▾) and chevrons (⌄) (yes, they’re strings) suggest expandability, but again, the text-overflow string isn’t clickable, so we can’t use it to unclip the overflowing text. However, we can make the element itself respond to :hover (or something):

Hover this container to reveal the full text.

div {
/* Size */
inline-size: 170px;

&:not(:hover) {
/* Prevent wrapping */
white-space: nowrap;

/* Hide any overflow */
overflow-inline: hidden;

/* Append string */
text-overflow: “▾”;
}
}

It’s not animatable, though. Sorry, not even @starting-style.

CodePen Embed Fallback

There’s a two-value syntax too

Huh? For what?

Well, remember how overflow can be set to anything but visible? Well, if the container is scrollable (overflow-inline: scroll, for instance), the two-value syntax can add ellipses/strings to both ends:

Hey, scroll this container.

div {
/* Size */
inline-size: 170px;

/* Prevent wrapping */
white-space: nowrap;

/* Hide any overflow, allow scrolling */
overflow-inline: scroll;

/* */
text-overflow: “///” ellipsis;
}

is what appears after the text, whereas is what appears before it. So, as we can see in the demo below, an ellipsis appears at the end of the text if there’s more of it to see, and the three forward slashes (///) appears at the start of the text if there’s more of it to see. If the text is overflowing the container at both ends, we’ll see the ellipsis and /// at the same time.

Only Firefox have implemented this two-value syntax:

CodePen Embed Fallback

Elements that are being scrolled or focused elements with editable content fall back to text-overflow: clip so that users can see the text in full when trying to interact with it. However, this behavior can differ between browsers.

Where to next?

text-overflow: enables us to look beyond ellipses, towards custom strings that indicate why the text was truncated. Also, the single-value syntax respects direction, so it’s logical too.

But that’s not all; we also have text-overflow: fade() and] fade on the way. text-overflow: fade(1em) would begin fading the line 1em from the end of the line box whereas text-overflow: fade(10%) would begin fading it 10% from the end, and in any case be completely transparent by the end. text-overflow: fade does the same thing but allows the user agent to determine the value.

The line-clamp property is essentially the same as text-overflow, except that it’s for multi-line text. It can, however, only truncate the last line (not the first line), and it doesn’t seem as if it’ll support either fade() or fade.

Either way, once all of their features are supported by web browsers, I think text-overflow and line-clamp will become quite useful. We’ve been playing with line-clamping for a long while now and it’s nice to have new tricks for it.

Source link

Similar Posts