Chrome 157 applied text-overflow: following Firefox 9, which was launched in, uhhh… 2011. Safari 27.3 Beta has additionally shipped it, so we will anticipate it to be Baseline quickly.
However what does text-overflow: do, precisely? Effectively, text-overflow: ellipsis provides an ellipsis (…) to the top of the road field, changing nevertheless many characters it takes up. That is in contrast to text-overflow: clip (the default worth), which provides nothing.
This textual content is overflowing its container.
div {
/* Dimension */
inline-size: 170px;
/* Stop wrapping */
white-space: nowrap;
/* Conceal any overflow */
overflow-inline: hidden;
/* Append string */
text-overflow: "—";
}
First, the textual content should overflow a block container in its inline path. That’s why I’ve declared inline-size: 170px in that final instance, which, assuming that the writing-mode computes to horizontal-tb, determines the width.
Subsequent, white-space: nowrap prevents textual content wrapping, inflicting overflow every time there’s no room for the textual content within the inline path. Sure, which means text-overflow is for single-line textual content (for multi-line textual content, you need line-clamp).
The overflow should be set to something however seen.
And at last, text-overflow: "—" provides an em sprint (—) to the top of the road field whereas truncating nevertheless many characters it takes up.
Notice that the string might be clean (text-overflow: ""). This truncates any overflowing characters with out changing them, in contrast to the default text-overflow: clip, which might go away fractional characters behind.
What ought to the string be?
The hot button is making it apparent that the textual content is truncated. We will, once more, use an em sprint as they’re are usually for interruptions and abrupt cut-offs of dialogue.
There’s text-overflow: "…", however that is just about the identical factor as text-overflow: ellipsis. Nonetheless, […] can be utilized to truncate quotes whereas signaling that it’s an editorial omission or design choice, not the top of the quoted textual content. If we would like it to be clickable, one thing akin to [read more] (with the sq. brackets wanting like a makeshift button) is a typical format. Nonetheless, the text-overflow string isn’t clickable, and since sq. brackets already imply one thing, I don’t suggest this format.
Different widespread bracketed interpolations embrace [truncated], [sic], [snip], and [excerpt], so text-overflow: lends itself to extra use circumstances than you may assume.
Carets (▾) and chevrons (⌄) (sure, they’re strings) counsel expandability, however once more, the text-overflow string isn’t clickable, so we will’t use it to unclip the overflowing textual content. Nonetheless, we can make the aspect itself reply to :hover (or one thing):
Hover this container to disclose the total textual content.
div {
/* Dimension */
inline-size: 170px;
&:not(:hover) {
/* Stop wrapping */
white-space: nowrap;
/* Conceal any overflow */
overflow-inline: hidden;
/* Append string */
text-overflow: "▾";
}
}
It’s not animatable, although. Sorry, not even @starting-style.
There’s a two-value syntax too
Huh? For what?
Effectively, keep in mind how overflow might be set to something however seen? Effectively, if the container is scrollable (overflow-inline: scroll, for example), the two-value syntax can add ellipses/strings to each ends:
Hey, scroll this container.
div {
/* Dimension */
inline-size: 170px;
/* Stop wrapping */
white-space: nowrap;
/* Conceal any overflow, permit scrolling */
overflow-inline: scroll;
/* */
text-overflow: "///" ellipsis;
}
is what seems after the textual content, whereas is what seems earlier than it. So, as we will see within the demo under, an ellipsis seems on the finish of the textual content if there’s extra of it to see, and the three ahead slashes (///) seems initially of the textual content if there’s extra of it to see. If the textual content is overflowing the container at each ends, we’ll see the ellipsis and /// on the identical time.
Solely Firefox have applied this two-value syntax:
Parts which can be being scrolled or centered components with editable content material fall again to text-overflow: clip in order that customers can see the textual content in full when attempting to work together with it. Nonetheless, this conduct can differ between browsers.
The place to subsequent?
text-overflow: permits us to look past ellipses, in the direction of customized strings that point out why the textual content was truncated. Additionally, the single-value syntax respects path, so it’s logical too.
However that’s not all; we even have text-overflow: fade() and] fade on the best way. text-overflow: fade(1em) would start fading the road 1em from the top of the road field whereas text-overflow: fade(10%) would start fading it 10% from the top, and in any case be fully clear by the top. text-overflow: fade does the identical factor however permits the consumer agent to find out the worth.
The line-clamp property is basically the identical as text-overflow, besides that it’s for multi-line textual content. It might probably, nevertheless, solely truncate the final line (not the primary line), and it doesn’t appear as if it’ll assist both fade() or fade.
Both manner, as soon as all of their options are supported by internet browsers, I believe text-overflow and line-clamp will turn out to be fairly helpful. We’ve been taking part in with line-clamping for an extended whereas now and it’s good to have new methods for it.









