CSS Ellipsis Beginning of String

By  on  
I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to accessibility. The CSS text-overflow: ellipsis feature is great but is essentially meant to ellipsize strings only at the end; what if we want to ellipsize the beginning of a screen?  The use case is fairly reasonable: think displaying a file path -- many times the directory for a set of files is the same, in which case you'd want to display the end of the string, not the beginning. Let me show you a trick for ellipsis at the begging of the string!

The CSS

Showing an ellipsis at the front of a string is mostly the same as ellipsis at the end, only with one simple trick:
.ellipsize-left {
    /* Standard CSS ellipsis */
    white-space: nowrap;                   
    overflow: hidden;
    text-overflow: ellipsis;  
    width: 200px;
    
    /* Beginning of string */
    direction: rtl;
    text-align: left;
}
To add an ellipsis at the beginning of a string, use RTL and and text-align to clip the beginning of the string! Playing RTL off of text-align is a genius way to get the desired effect of CSS ellipsis at the beginning of an element or string.  It would be great for the CSS spec to implement a more robust ellipsis system but, for now, I worship amazing CSS tricks like this!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Create a Clearable TextBox with the Dojo Toolkit

    Usability is a key feature when creating user interfaces;  it's all in the details.  I was recently using my iPhone and it dawned on my how awesome the "x" icon is in its input elements.  No holding the delete key down.  No pressing it a...

  • By
    Create Keyboard Shortcuts with Mousetrap

    Some of the finest parts of web apps are hidden in the little things.  These "small details" can often add up to big, big gains.  One of those small gains can be found in keyboard shortcuts.  Awesome web apps like Gmail and GitHub use loads of...

Discussion

  1. The CSS spec seems to recommend against using the direction property on web pages:

    https://drafts.csswg.org/css-writing-modes-3/#direction

  2. yak613

    http://jsfiddle.net/yak613/fhr2s10c/

    This seems kind of strange. Where is the extra slash coming from?

  3. Boon

    This trick seems to be broken for Safari which still truncates from the back then appends the ellipsis to the front.
    Chrome/FF: 12345 => …345
    Safari: 12345 => …123

  4. Jon Wallsten

    For anyone having issues with symbols, like the plus sign in international phone numbers, add this:
    unicode-bidi: plaintext;

  5. If anyone dealing with multiline strings to truncate i recommend using the cuttr.js (https://github.com/d-e-v-s-k/cuttr-js) library ;)

  6. Dan

    Just in case anyone else runs into this… I had an issue where if the text contained punctuation, adding

    direction: rtl

    moved the punctuation marks to the beginning of the text. I solved this by appending the unicode ‎ character to the end of the string with an :after

    .ellipsize-left {
        /* Standard CSS ellipsis */
        white-space: nowrap;                   
        overflow: hidden;
        text-overflow: ellipsis;  
        width: 200px;
        
        /* Beginning of string */
        direction: rtl;
        text-align: left;
    }
    .ellipsize-left:after {
        content: '\200E'
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!