10

Consider this html/css snippet:

.l { text-align: left; }
.r { text-align: right; }

p { 
  width: 150px; 
  overflow: hidden; 
  white-space: nowrap; 
  text-overflow: ellipsis;
  border: solid 1px green; 
}
<p class="l">111222333444555666777888999</p> 
<p class="r">111222333444555666777888999</p>

It shows two fixed-width containers with some text too long to fit, with overflow set to show an ellipsis to show that some text is hidden. The first container is left justified, the second is right justified.

The result shows that the ellipsis is on the right for both examples.

However, for the second right justified one, I'd like to achieve this:

...4555666777888999

instead of

1112223334445556...

Is this possible?

BG100
  • 4,481
  • 2
  • 37
  • 64

2 Answers2

21

You can set the direction of text from right to left using css direction property direction: rtl:

.l {
  text-align: left;
  direction: rtl;
}
.r {
  text-align: right;
}
p {
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  border: solid 1px green;
}
<p class="l">111222333444555666777888999</p>
<p class="r">111222333444555666777888999</p>

direction

Set the direction CSS property to match the direction of the text: rtl for languages written from right-to-left (like Hebrew or Arabic) text and ltr for other scripts. This is typically done as part of the document (e.g., using the dir attribute in HTML) rather than through direct use of CSS.

References

MDN direction

Alex Char
  • 32,879
  • 9
  • 49
  • 70
  • 3
    IE11 shows `...111222333` instead of `...4555666` – Benoit Blanchon Feb 14 '18 at 11:53
  • This is unfortunately not a general purpose solution. `

    0.example.com

    ` renders as `example.com.0` in Firefox and Chrome.
    – Prinzhorn Feb 03 '20 at 10:07
  • Found a solution: start your content with `‎` to force the content itself to be rendered as regular left-to-right. https://stackoverflow.com/questions/27957443/strange-special-characters-issue-in-left-side-ellipsis – Prinzhorn Feb 03 '20 at 10:10
  • Safari also displays `...1112223334445556` -_-||| – kayochin Jan 09 '23 at 02:55
2

To get this effect you have to use a little hack. See the following example:

p {
  border:1px solid #000;
  width:150px;
}
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reverse-ellipsis {
  text-overflow: clip;
  position: relative;
  background-color: white;
}
.reverse-ellipsis:before {
  content: '\02026';
  position: absolute;
  z-index: 1;
  left: -1em;
  background-color: inherit;
  padding-left: 1em;
  margin-left: 0.5em;
}

.reverse-ellipsis span {
  min-width: 100%;
  position: relative;
  display: inline-block;
  float: right;
  overflow: visible;
  background-color: inherit;
  text-indent: 0.5em;
}
.reverse-ellipsis span:before {
  content: '';
  position: absolute;
  display: inline-block;
  width: 1em;
  height: 1em;
  background-color: inherit;
  z-index: 200;
  left: -.5em;
}
<p class="ellipsis reverse-ellipsis">
  <span>111222333444555666777888999</span>
</p>
<p class="ellipsis">111222333444555666777888999</p>

More information about this you can find here: http://hugogiraudel.com/2014/12/16/css-riddle-reverse-ellipsis/

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87