Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text-overflow ellipsis on left side

I have a list of paths (for lack of a better word, maybe bread crumb trails describes them better). Some of the values are too long to display in their parent so I'm using text-overflow: ellipsis. The problem is that the important information is on the right, so I'd like the ellipsis to appear on the left. Something like this this ascii art:

---------------------------- |first > second > third    | |...second > third > fourth| |...fifth > sixth > seventh| ---------------------------- 

Notice that the first row is short enough so it remains left aligned, but the other two are too long so the ellipsis appears on the left hand side.

I'd prefer a CSS only solution, but JS is fine if it can't be avoided. It's ok if the solution only works in Firefox and Chrome.

EDIT: At this point I'm looking for a work around for the bugs in Chrome that prevent it from rendering properly when a document is mixed RTL and LTR. That was all I really needed from the outset, I just didn't realize it.

like image 389
Hemlock Avatar asked Mar 20 '12 19:03

Hemlock


People also ask

How do you change text overflow on ellipsis?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

What does text overflow ellipsis mean?

Definition and Usage The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.


2 Answers

How about something like this jsFiddle? It uses the direction, text-align, and text-overflow to get the ellipsis on the left. According to MDN, there may be the possibility of specifying the ellipsis on the left in the future with the left-overflow-type value however it's considered to still be experimental.

p {    white-space: nowrap;    overflow: hidden;    /* "overflow" value must be different from "visible" */    text-overflow: ellipsis;    width: 170px;    border: 1px solid #999;    direction: rtl;    text-align: left;  }
<p>first > second > third<br /> second > third > fourth > fifth > sixth<br /> fifth > sixth > seventh > eighth > ninth</p>​
like image 82
j08691 Avatar answered Sep 20 '22 18:09

j08691


I finally had to crack and do something in JavaScript. I was hoping that someone would come up with a hail-mary CSS solution but people seem to just be up-voting the answer that should be correct if it weren't for the Chrome bugs. j08691 can have the bounty for his work.

<html>     <head>         <style>             #container {                 width: 200px;                 border: 1px solid blue;             }              #container div {                 width: 100%;                 overflow: hidden;                 white-space: nowrap;             }         </style>         <script>             function trimRows() {                  var rows = document.getElementById('container').childNodes;                 for (var i=0, row; row = rows[i]; i++) {                     if (row.scrollWidth > row.offsetWidth) {                         var textNode = row.firstChild;                         var value = '...' + textNode.nodeValue;                         do {                             value = '...' + value.substr(4);                             textNode.nodeValue = value;                          } while (row.scrollWidth > row.offsetWidth);                     }                 }             }         </script>     </head>     <body onload='trimRows();'>     <div id="container" >         <div>first > second > third</div>         <div>second > third > fourth > fifth > sixth</div>         <div>fifth > sixth > seventh > eighth > ninth</div>​     </div>     </body>  </html> 

Fiddle

like image 41
Hemlock Avatar answered Sep 18 '22 18:09

Hemlock