4

How to display only last part of string, if it more than my div width without line breaks? E.g. "...string" instead of

This is my
very large string

UPD: This solution works, but when i have a large word, e.g. url http://myhost/path/foo/bar?and=some&params=there displays only part of symbol, it is not good. Any ideas?

3
  • 1
    hugogiraudel.com/2014/12/16/css-riddle-reverse-ellipsis Commented Feb 16, 2016 at 12:44
  • thanks, it is works, but not in all cases. Please, see updated question fore more information. Commented Feb 16, 2016 at 13:03
  • No, it's doubtful if it would work in all cases. It's not a standard request. Commented Feb 16, 2016 at 13:06

1 Answer 1

4

You can do this with direction: rtl; but its not going to work in Chrome

div {
  white-space: nowrap; 
  padding: 0 10px;
  width: 50px; 
  overflow: hidden;
  text-overflow: ellipsis; 
  border: 1px solid #000000;
  direction: rtl;
}
<div>This is my
very large string</div>

Or you can use JQuery and take last word from text, also the width of p will be dynamic and adjust to width of last word.

var part = $('p').text().split(' ');
$('p').text('... '+part[part.length-1]);
p {
  white-space: nowrap; 
  padding: 0 10px;
  overflow: hidden;
  display: inline-block;
  border: 1px solid #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is my
very large string</p>

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately, my users use Chrome mostly(
hmm seems to work in Chrome on 24 november 2017... does this mean that the top version is the recommended way? I like CSS solution above JQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.