Steffen Hi, the name's Steffen and I'm writing about the Web, programming and all those things coming to my mind. Enjoy your stay.

I'm currently working on fabidoo.com - 3D Printing for everyone!

Ellipsis or “truncate with dots” via JavaScript

Internet Explorer comes with a nice CSS feature for truncating text by appending three little dots: text-overflow:ellipsis. Here’s a screen shot:

IE Ellipsis

Unfortunately text-overflow:ellipsis is no standard and not supported by Firefox, Opera, etc. With some JavaScript hacking however you can have something similar to IE ellipsis on all browsers. It even degrades gracefully if JavaScript is disabled.

My solution allows you to simply add a class to an HTML element to make it text-overflow ellipsis. It’s as simple as

<h3 class="ellipsis">The is a heading that gets cut off</h3>

All you have to do in advance is to include ellipsis.js and prototype.js. Jep, I’m using Prototype - but it should be pretty trivial to port it to other JavaScript libraries.

Here’s a demo and here’s the script - MIT-style licensed and tested on IE 6+7, FF 2, Safari 2, Opera 9.

Some Insights

The graceful degradtion is done via a CSS trick. An ellipsis element will have a negative margin of -10000px. It’s like setting its width to some large value, except you get around the Expanding Box bug of Internet Explorer 6. Yeah, I actually decided to not use IEs ellipsis feature, even if I’m on IE - it is pretty nasty to work with (e.g. you have to directly apply a width on ellipsised elements - yuck).

The CSS rule for the ellpisis class is injected into your HTML document via document.write. Here’s the snippet taken from ellipsis.js:

  document.write('<style type="text/css"&gt' +
    '.ellipsis { margin-right:-10000px; }</style>');
  Event.observe(window, "load", function() {
    $$('.ellipsis').each(ellipsis);
  });

The snippet also shows, that once the document is loaded in the browser, the ellipsis function is applied on every element having the ellipsis class. And this function actually contains the JavaScript magic:

function ellipsis(e) {
  var w = e.getWidth() - 10000;
  var t = e.innerHTML;
  e.innerHTML = "<span>" + t + "</span>";
  e = e.down();
  while (t.length > 0 && e.getWidth() >= w) {
    t = t.substr(0, t.length - 1);
    e.innerHTML = t + "...";
  }
}

The function calculates the target width of the element by subtracting the negative margin. Then it will inject a span child containing the original text content of the element. The text in this span will then be truncated until the width of the injected span is smaller than the target width of our element - of course including the three dots ;-) And that’s it…

Drawbacks

  • Since the CSS class for ellipsis is injected via JavaScript, if you want the graceful degredation feature, you have to add the following line manually into your stylesheet:
    .ellipsis { margin-right:-10000px; }
    
  • Never set a right margin on an ellipsis element as my script requires those elements to have a right margin of -10000px, which is set via the inline CSS style.
  • Due to the negative margin, backgrounds, etc. on ellipsis elements will grow to the right - use overflow:hidden on one of the elements containers as a workaround.
  • Resizing ellipsis elements won’t render the dots anew - the layout can not by dynamic.

8 Responses to “Ellipsis or “truncate with dots” via JavaScript”

  • 1
    john z Says:

    cool script. I have two suggestions. Implement an alt parameter that contains all of the original text so when the cursor hoovers over the truncated text, the full text is displayed. The other would be to include a print version where the class then takes the full text.

    Regards

    John Z

  • 2
    Sean O Says:

    Nice code snippet. For a lean solution, have a look at the Truncate plugin for jQuery:
    http://reindel.com/truncate/

    Only 1k (compressed) — 20k, including all the jQuery goodness — with inline show/hide to boot.

  • 3
    pixelchutes Says:

    Does this script account for HTML entities?

    e.g. if the the truncation tolerance occurs within an  , will it show “&nb…” Make sense?

  • 4
    kenneth Says:

    How about substituting “…” with … (Horizontal Ellipsis)? :)

  • 5
    Ben Marklein Says:

    Sean O - sorry, that jquery script is inferior. It truncates to a number of characters, which is trivial. The script above truncates based on actual _width_. If you truncated to a number of characters and you want to ensure that the text will fit within a given width, you need to base your calculation off of the widest character, which in reality may be some multiple of your narrowest character, e.g. “W” vs. “i”.

  • 6
    Tamer Says:

    Another thing is what about Resizing the browser, if i have a a text in a TD with width=”80%” the script works like a charm in normal state of the browser window BUT if i maximize the browser then the Ellipsis is meaningless since there will be a big white space after the text that could be used, what would be the implementation then ?

  • 7
    lahmatiy Says:

    Safari3 support text-overflow.
    Opera9 support text-overflow via -o-text-overflow.
    In Firefox we can use -moz-binding.
    Look this solution http://basis.et-lab.ru/habrahabr/ellipsis/step4.html or http://basis.et-lab.ru/habrahabr/ellipsis/demo.html

  • 8
    Jonah Dempcy Says:

    My colleague Alex posted an article about truncating in JavaScript using the Prototype framework:

    http://www.thetruetribe.com/2008/05/how-to-truncate-in-prototype.html

  • Leave a Reply

18411