Truncating long strings with CSS: feasible yet?
Solution 1
Update: text-overflow: ellipsis
is now supported as of Firefox 7 (released September 27th 2011). Yay! My original answer follows as a historical record.
Justin Maxwell has cross browser CSS solution. It does come with the downside however of not allowing the text to be selected in Firefox. Check out his guest post on Matt Snider's blog for the full details on how this works.
Note this technique also prevents updating the content of the node in JavaScript using the innerHTML
property in Firefox. See the end of this post for a workaround.
CSS
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;
-moz-binding: url('assets/xml/ellipsis.xml#ellipsis');
}
ellipsis.xml
file contents
<?xml version="1.0"?>
<bindings
xmlns="http://www.mozilla.org/xbl"
xmlns:xbl="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
>
<binding id="ellipsis">
<content>
<xul:window>
<xul:description crop="end" xbl:inherits="value=xbl:text"><children/></xul:description>
</xul:window>
</content>
</binding>
</bindings>
Updating node content
To update the content of a node in a way that works in Firefox use the following:
var replaceEllipsis(node, content) {
node.innerHTML = content;
// use your favorite framework to detect the gecko browser
if (YAHOO.env.ua.gecko) {
var pnode = node.parentNode,
newNode = node.cloneNode(true);
pnode.replaceChild(newNode, node);
}
};
See Matt Snider's post for an explanation of how this works.
Solution 2
2014 March: Truncating long strings with CSS: a new answer with focus on browser support
Demo on http://jsbin.com/leyukama/1/ (I use jsbin because it supports old version of IE).
<style type="text/css">
span {
display: inline-block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /** IE6+, Firefox 7+, Opera 11+, Chrome, Safari **/
-o-text-overflow: ellipsis; /** Opera 9 & 10 **/
width: 370px; /* note that this width will have to be smaller to see the effect */
}
</style>
<span>Some very long text that should be cut off at some point coz it's a bit too long and the text overflow ellipsis feature is used</span>
The -ms-text-overflow CSS property is not necessary: it is a synonym of the text-overflow CSS property, but versions of IE from 6 to 11 already support the text-overflow CSS property.
Successfully tested (on Browserstack.com) on Windows OS, for web browsers:
- IE6 to IE11
- Opera 10.6, Opera 11.1, Opera 15.0, Opera 20.0
- Chrome 14, Chrome 20, Chrome 25
- Safari 4.0, Safari 5.0, Safari 5.1
- Firefox 7.0, Firefox 15
Firefox: as pointed out by Simon Lieschke (in another answer), Firefox only support the text-overflow CSS property from Firefox 7 onwards (released September 27th 2011).
I double checked this behavior on Firefox 3.0 & Firefox 6.0 (text-overflow is not supported).
Some further testing on a Mac OS web browsers would be needed.
Note: you may want to show a tooltip on mouse hover when an ellipsis is applied, this can be done via javascript, see this questions: HTML text-overflow ellipsis detection and HTML - how can I show tooltip ONLY when ellipsis is activated
Resources:
- https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow#Browser_compatibility
- http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/
- https://stackoverflow.com/a/1101702/759452
- http://www.browsersupport.net/CSS/text-overflow
- http://caniuse.com/text-overflow
- http://msdn.microsoft.com/en-us/library/ie/ms531174(v=vs.85).aspx
- http://hacks.mozilla.org/2011/09/whats-new-for-web-developers-in-firefox-7/
Solution 3
If you're OK with a JavaScript solution, there's a jQuery plug-in to do this in a cross-browser fashion - see http://azgtech.wordpress.com/2009/07/26/text-overflow-ellipsis-for-firefox-via-jquery/
Solution 4
OK, Firefox 7 implemented text-overflow: ellipsis
as well as text-overflow: "string"
. Final release is planned for 2011-09-27.
Solution 5
Another solution to the problem could be the following set of CSS rules:
.ellipsis{
white-space:nowrap;
overflow:hidden;
}
.ellipsis:after{
content:'...';
}
The only drawback with the above CSS is that it would add the "..." irrespective of whether the text-overflows the container or not. Still, if you have a case where you have a bunch of elements and are sure that content will overflow, this one would be a simpler set of rules.
My two cents. Hats off to the original technique by Justin Maxwell
Related videos on Youtube
Sam Stokes
Software developer, programming language nerd, CTO of Rapportive. Follow me on Twitter.
Updated on January 12, 2020Comments
-
Sam Stokes over 4 years
Is there any good way of truncating text with plain HTML and CSS, so that dynamic content can fit in a fixed-width-and-height layout?
I've been truncating server-side by logical width (i.e. a blindly-guessed number of characters), but since a 'w' is wider than an 'i' this tends to be suboptimal, and also requires me to re-guess (and keep tweaking) the number of characters for every fixed width. Ideally the truncation would happen in the browser, which knows the physical width of the rendered text.
I've found that IE has a
text-overflow: ellipsis
property that does exactly what I want, but I need this to be cross-browser. This property seems to be (somewhat?) standard but isn't supported by Firefox. I've found various workarounds based onoverflow: hidden
, but they either don't display an ellipsis (I want the user to know the content was truncated), or display it all the time (even if the content wasn't truncated).Does anyone have a good way of fitting dynamic text in a fixed layout, or is server-side truncation by logical width as good as I'm going to get for now?
-
Adrien Be almost 10 years2014: see an up-to-date answer stackoverflow.com/questions/802175/…
-
Michael Benjamin over 8 yearsrelated: Applying Ellipsis to Multiline Text
-
Naeem Shaikh over 8 yearsPossible duplicate of Insert ellipsis (...) into HTML tag if content too wide
-
-
Sam Stokes about 15 yearsThat's definitely useful, thanks! Seems like all the browsers except Firefox support the CSS property, so with this plugin, the only people it wouldn't work for are Firefox users who've disabled Javascript - and it's a graceful degradation anyway. Any idea what the performance implications are like?
-
RichieHindle about 15 yearsNo, sorry... I haven't used the code in real life, just played with the demo. It would be easy to take the demo and cut-and-paste it a hundred times into the same page.
-
Sam Stokes almost 15 yearsThat's awesome, thanks for pointing it out! The unselectable text and restrictions on what content can go in the truncated div are a shame, but generally that looks like a good solution.
-
Matchu almost 15 yearsOnly real frustration I've hit is that spaces are rendered as , so indentation isn't really feasible... =/
-
mcjabberz almost 15 yearsI ran across this same problem too. I can't believe Firefox doesn't support this in some form yet.
-
Matthias almost 15 yearsJavaScript truncate() (be it dot-string for jQuery or Prototype or whatever) are only a half-way solution, because they do not take into account the character width. So, if you want to truncate text because of a pre-defined limited space, those functions only work reliably when using monospaced fonts. Any serious solution would have to operate on glyphs, not on character count.
-
Rajat about 14 yearsdoes this approach work for anyone on OPTION elements of SELECT controls on Webkit and IE8. Webkit doesnt seem to be doing anything for me and IE8 just clips it without the ellipsis.
-
Simon Lieschke about 14 yearsMicrosoft's documentation for
text-overflow
doesn't indicate support foroption
elements (see the Applies To section at msdn.microsoft.com/en-us/library/ms531174(VS.85).aspx). -
RichieHindle almost 14 years@Matthias: Perhaps the code has been updated since you tested it, but I've just looked at the demo and it works perfectly with a variable-width font.
-
Simon Lieschke almost 14 yearsJust a heads up that the XUL hack currently doesn't work in Firefox 4.0 (which is in development as of writing). I'll keep people updated. bugzilla.mozilla.org/show_bug.cgi?id=312156#c94
-
Simon Lieschke about 13 yearsThe ETA for Firefox support for this (as of writing) is Firefox 6.
-
Sam Soffes about 13 yearsWorks great! I didn't implement the Firefox stuff for my project since it was annoying. Screw the Firefox users :)
-
Simon Lieschke almost 13 years
text-overflow: ellipsis
is now supported as of Firefox 7. Yay! -
Simon East over 12 yearsDoesn't work for table cells I noticed. You need to wrap the content inside a DIV and apply the style to the DIV, not the table cell (in Chrome/Webkit at least).
-
Simon Lieschke over 12 years@Simon also see this answer.
-
Antony over 12 yearsThe problem with pseudo code is that "..." still gets cut off or hidden if text does overflow. I was hoping that if text overflows, it would make sure "..." to be shown. Clearly, that's not the case :(
-
last-child about 11 years@Antony Just position the pseudo element:
position: absolute; right: 0;
(and don't forgetposition: relative
on the real element for it to work). It will overlap with the text though so you might want to add a background color too. -
Adrien Be almost 8 years@chiragpatel try it yourself by editing this live demo jsbin.com/leyukama/1
-
Tom Tom about 7 yearsFor anyone interested FF7< is 0,05% of Firefox users nowadays