[css-overflow-4] Ellipsizing of text in middle of string #3937
Description
Hello,
There are many cases in which the information at the beginning & end (but not middle) of a string of characters are important, especially in technical identifiers (e.g. URLs, file names, library call numbers, etc.). It would be wonderful if this was supported in CSS itself. Attempts to do with the current tools available are unattractive and complex at best.
Apologies if this is better addressed in the mailing list--the guidelines and other material online were unclear which is preferred in cases such as these.
I am following on from a previous inquiry led by Sebastian Zartner, to whom I owe thanks. His www-style@w3.org
thread from 2013 ended without any conclusions or next steps. Note that I am not the author of the detailed proposal listed below, but merely a web developer interested in highlighting this proposal.
Metadata
Assignees
Type
Projects
Status
Unslotted
Status
Unsorted regular
Activity
SebastianZ commentedon May 18, 2019
Thank you for picking up my proposal, Harrison!
For reference, my proposal actually included two ideas. The first one was to allow cropping in the middle of the element's content and the second, related one to preserve some part of the contents on smaller element widths.
To avoid discussing two features at the same time, let's focus solely on middle-cropping here.
My proposal back then was to reuse
text-overflow
for this purpose and simply add a third value to it specifying the cropping in the center of the contents. @MatsPalmgren had another proposal in the related enhancement request for Gecko, which let you move the end ellipsis. Both proposals turnedtext-overflow
into a shorthand and introduced different longhands allowing to control the different partial values separately.Issues with the proposals, which need to be solved:
clip
should not be allowed for it.Sebastian
SebastianZ commentedon May 18, 2019
Additional note: The proposal only covers inline content cropping. There might be cases, in which you'd want to do block-level middle-cropping. Though I guess that's rather related to
line-clamp
.Sebastian
frivoal commentedon May 20, 2019
Would there? on single line ellipsis, doing it it the middle makes sense to me, but on multiline, it seems a lot less useful. Maybe I'm just short on imagination. Do you have examples of when you'd want that?
ghost commentedon May 28, 2019
I would appreciate this feature as well. macOS Finder uses an inner ellipsis likely because the most variation and differentiation in a filename is at the beginning and end of the filename.
jonjohnjohnson commentedon May 28, 2019
Here's the utility I use to solve this gap in the spec. But it does have the caveats of not working in webkit, misusing
direction
, and being tricky to semantically style (em
/strong
) text across the center boundary, but it is selectable and mostly accessible. :/https://jsfiddle.net/t5jvns7q/
SebastianZ commentedon May 29, 2019
Well, the cases are not as strong as the single-line cases, I guess. Though the principle stays the same, i.e. cases, in which you care more about the ending than the middle of the contents.
One use case could be the Inspector panel within browser DevTools, which shows the text contents of the elements. When they span over many lines, it gets quickly hard to skim over the HTML structure. Those could limit the contents to a specific number of lines but crop them in the middle of the contents, so it's easy to skim over them.
Example:

Could be shrinked to someting like in this mockup:
Sebastian
SebastianZ commentedon Jul 6, 2023
Giving the issues stated earlier some more thought, I came up with another approach than outlined in my first suggestion.
We might introduce a
text-overflow-position
longhand to cover that.Some possible options to handle this:
auto
value and make it the initial value. For start and end positioning of the overflow handling, the used value would then beclip
, for middle positioning it would beellipsis
. (That requires checking web compatibility.)clip
also apply to middle positioning. (Though clipping the inline content in the middle probably doesn't have any use cases.)clip
for middle positioning asellipsis
. (Might be unexpected.)clip
for middle positioning as if end positioning was applied. I.e. clip the contents at the edge where they overflow the element.This would work very similar to the current definition. I.e. characters and atomic inline-level elements would be removed until the ellipsis fits.
With an
auto
value as suggested above, the syntaxes might then look like this:text-overflow
=<'text-overflow-handling'> || <'text-overflow-position'>
text-overflow-handling
=auto | [ clip | ellipsis | <string> | fade | <fade()> ]{1,2}
text-overflow-position
=[ start || end ] | middle
With names to be bikeshedded, as always. And only the first handling value would be applied if middle positioning is chosen.
The simplest middle-cropping could then be achieved via
text-overflow: middle
. Some advanced examples would betext-overflow: middle fade(3ch)
andtext-overflow: start end ellipsis
.Doing so, we'd have to define what to do in cases like
text-overflow: end start ellipsis fade
.Sebastian
SebastianZ commentedon Jul 19, 2023
The initial suggestions celebrated 10 years anniversery yesterday. 😄
Maybe we can discuss the latest suggestion while we're at the F2F.
Sebastian
13 remaining items
css-meeting-bot commentedon Apr 24, 2024
The CSS Working Group just discussed
[css-overflow-4] Ellipsizing of text in middle of string
, and agreed to the following:RESOLVED: Start work on middle ellipsis in css-overflow-4
The full IRC log of that discussion
<fantasai> astearns: are we going to do this, or punt it?<fantasai> SebastianZ: Longstanding thing I proposed 10 years ago, a way to crop content of an element in the middle instead of at the beginning or end
<fantasai> ... proposal went through some iterations
<fantasai> ... current idea is to make text-overflow a shorthand and introduce text-overflow-handling and text-overflow-position
<SebastianZ> https://github.com//issues/3937#issuecomment-1624382903
<fantasai> text-overflow = <'text-overflow-handling'> || <'text-overflow-position'>
<fantasai> text-overflow-handling = auto | [ clip | ellipsis | <string> | fade | <fade()> ]{1,2}
<fantasai> text-overflow-position = [ start || end ] | middle
<fantasai> SebastianZ: we cover the current behaviors, plus cropping
<fantasai> SebastianZ: also outlined rudimentary algorithm later on
<florian_irc> q+
<kizu> q+
<fantasai> SebastianZ: anyone on the call have comments?
<astearns> ack florian_irc
<fantasai> florian_irc: my main concern here is the handling of bidi
<fantasai> florian_irc: in general, just like in start/end of line, should do visual cropping not logical
<fantasai> ... logical cropping is going to be having some strange cropping
<fantasai> s/cropping/questions/
<fantasai> ... in generalized case, including bidi, makes me worry
<fantasai> ... because when you chop start/end, clear what happens, but if you crop middle you have to move the other parts
<fantasai> ... suspect it's more complex than we wish
<astearns> ack kizu
<fantasai> kizu: I don't have any opinion on bidi, but ellipsing in the middle is something I've wanted for more than 10 years
<jfkthame> q+
<fantasai> kizu: Last time, there was concern about not allowing clipping in the middle
<fantasai> kizu: but still good way to do it, if it's exactly in the middle then author can position something interesting there
<fantasai> kizu: but this will require browser to know
<fantasai> kizu: other than that, lots of people want this
<fantasai> kizu: idk if in this issue or other, if we need an ellipsis in multiline
<fantasai> kizu: in our product we need this, but should discuss separately
<fantasai> kizu: but for single-line, a lot of use cases
<astearns> ack jfkthame
<fantasai> jfkthame: I still disagree with Florian that visual ellipsizing in the middle is the right approach for bidi
<fantasai> jfkthame: in most cases, the most important content will be the beginning of the text and/or the end of the text logically speaking
<fantasai> jfkthame: so those pieces should be preserved
<fantasai> jfkthame: I wonder if, given the complexity/uncertainty, is define middle as a feature but leave the behavior of bidi cases for browsers to experiment with initially
<dbaron> Scribe+
<fantasai> jfkthame: until we see what works in real-world use case
<astearns> ack fantasai
<SebastianZ> +1 to what Jonathan said.
<dbaron> fantasai: I ithnk this makes sense -- the question is whether there's implementor interest. If so, we should spec it.
<dbaron> s/ithnk/think/
<dbaron> astearns: do you want to gate specifying on implementor interest? Or should we start it, mark it at risk, and work on spec in parallel?
<fantasai> astearns: should we gate spec on implementer interest?
<fantasai> florian_irc: in this case, I think yes
<fantasai> florian_irc: we agree that this is hard, and we don't know enough to spec it
<fantasai> florian_irc: so even if we did spec it, we wouldn't be able to get interop-level detail
<fantasai> astearns: even if there's open questions, the majority of use cases aren't bidi
<andreubotella> q+
<fantasai> astearns: it's something devs need and have a use for
<florian_irc> q+
<astearns> ack andreubotella
<fantasai> andreubotella: Chromium has an implementation of ellipsis in the middle that they use for file selection
<fantasai> andreubotella: I don't know what it does wrt bidi, but they have an implementation
<astearns> ack florian_irc
<fantasai> florian_irc: even if not bidi, there's still ambiguity wrt visual or logical middle
<fantasai> florian_irc: if non-monospace font, where is the middle?
<fantasai> florian_irc: effect of disagreeing will be less dramatic, but still will have disagreement
<SebastianZ> q+
<fantasai> florian_irc: but maybe useful enough to spec
<fantasai> florian_irc: would prefer to wait until interest, but not an objection
<astearns> ack SebastianZ
<fantasai> SebastianZ: There was at some point an implementation in XUL
<fantasai> SebastianZ: but they removed it at some point
<fantasai> dholbert: we replaced it with another hack based on HTML+CSS
<astearns> ack SebastianZ
<fantasai> dholbert: we have something, but idk how good it is. Similar to Chromium
<fantasai> astearns: given that there does appear to be something implemented, is that enough?
<fantasai> fantasai: can't speak for Apple, but doubt we'd object
<fantasai> jfkthame: unsure when we'd get to it, but some interest
<fantasai> jfkthame: we know that what we've got doesn't handle i18n well
<fantasai> jfkthame: and partly lack of a spec has discouraged us from being more thorough about integrating with CSS
<fantasai> astearns: I'm inclined to resolve that we start the spec of this, knowing it will be complicated, and it may take awhile
<fantasai> florian_irc: Do we start with SebastianZ's proposal of two longhands?
<fantasai> astearns: seems like a good place to start
<fantasai> fantasai: probably need to bikeshed -handling at some point
<fantasai> astearns: proposed to start?
<fantasai> RESOLVED: Start work on middle ellipsis in css-overflow-4
dbaron commentedon Apr 24, 2024
For what it's worth, one thing I was going to add to the discussion regarding bidi is that my intuition about real bidi usecases (which are different from the testcases we write!) is that real bidi use cases generally have a dominant text direction and small pieces of text in other directions.
I think that might make me lean towards the idea that visual is fine for the middle like it is for the ends, but I'm also not really an expert on the use cases...
yisibl commentedon Jun 26, 2024
Yes, the
vue-clamp
component implements the ellipsis position, which takes values'start' | 'middle' | 'end'
, this on github currently has close to 700 stars. cc @Justineohttps://vue-clamp.vercel.app/#demo
nico3333fr commentedon Oct 15, 2024
Another case we have in Proton Drive: we want to keep the last characters to display the extension of a file (with long filename, for security reasons)
We did implement a solution by splitting the filename thanks to this component https://github.com/ProtonMail/WebClients/blob/main/packages/components/components/ellipsis/MiddleEllipsis.tsx but when we have a mix of RTL/LTR, it's a nightmare.
A native solution with an offset would be really helpful there :)
EDIT: some explanations of the issues => https://www.smashingmagazine.com/2022/12/deploying-css-logical-properties-on-web-apps/#mixing-rtl-and-ltr-content