Hacker News new | ask | show | jobs
by blago 5480 days ago
I agree, it would be a lot more useful. Safari has an unsupported property (-webkit-line-clamp) that goes a little further but still limited. The fact that none of the browsers provides a way (no even vendor-prefixed) to do it makes me think that there are big technical hurdles.

I needed this desperately for a project I worked on last fall and ended up writing tons of JS which felt oh so wrong. Newspapers and magazines would LOVE to have that. Especially on mobile Safari. Especially on tablets.

2 comments

I imagine it's simply complex. Part of the box model involves expanding containers to fit the content within them, and properties like float, overflow, width, and height run counter to that. So what do you do with "expand this div to fit the text it contains, but also allow the text to be truncated if it doesn't fit, and remember to leave room for the ellipses"?

When you get into fancier concepts like truncating a set of lines, or only at certain word/phrase/sentence boundaries, you're into desktop publishing typesetting. To do that properly you're not just truncating when the container overflows, you're also adjusting the spacing between words and letters to make every line look consistent and to get as much of the text to fit neatly as you can. There aren't many pieces of software that professionals trust to do that well, and the ones that exist are expensive. We're not likely to see those algorithms getting into everyone's browser.

> and remember to leave room for the ellipses

As long as the container is at least wide enough for one ellipses you shouldn't need to leave room: once you've decided where to place all the text, where an ellipses is needed overwrite the area with what would otherwise be the background and add the dots.

Though I suppose that might not be desirable as it would result in partial characters. A little more hassle (but not much) would be to work back until the end of the next whole character.

But other than that you are right: the difficulty won't be rendering the ellipsis it will be in deciding what to do when the (implicit or explicit, and quite possibly deeply nested) "make this all fit if you can" and "truncate this if you need to" directives overlap - that could get quite hairy, if you want to get something that looks nice for most conceivable combinations.

Or it can be quite simple. Note that we are talking about overflow: text that given the CSS on the page would become invisible. This knowledge is already present in the rendering subsystem because it has to decide what gets displayed.

The only trick is to open up enough space at the end of the line to render the ellipsis. It's not rocket science to do it well enough.

It can't be a completely intractable problem; iPhone apps have been able to do it since iOS 2.0.