Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS styling to show end of single text string

Tags:

html

css

overflow

I have a long text string. How do I get all the text to show on one line and with a certain width, but limit what is shown to the end of the string?

.demo {
    border: 1px solid red;
    white-space: nowrap;
    max-width: 100px;
    overflow: hidden;
}
<p class="demo">hello this is a string</p>

Here it shows the beginning of the string and cuts off the end, but I need it to show the end and cut off the beginning.

like image 858
Jake Avatar asked Jan 27 '18 22:01

Jake


People also ask

How do you trim a string in CSS?

AFAIK: There's no way to do that in CSS. The only way to achieve what you want is using some script language (e.g. JavaScript). Save this answer.

How do I view content on one line?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.

How do you wrap text-overflow?

The overflow-wrap CSS property applies to inline elements, setting whether the browser should insert line breaks within an otherwise unbreakable string to prevent text from overflowing its line box.


2 Answers

Here is a flex solution without changing direction:

.demo {
  border: 1px solid red;
  white-space: nowrap;
  max-width: 100px;
  overflow: hidden;
  display: flex;
  justify-content: flex-end;
}
<p class="demo">hello this is a string</p>
like image 156
Temani Afif Avatar answered Sep 28 '22 10:09

Temani Afif


.demo {
    border: 1px solid red;
    white-space: nowrap;
    max-width: 100px;
    overflow: hidden;
    direction: rtl;
}
<p class="demo">hello this is a string</p>
like image 32
Rauli Rajande Avatar answered Sep 28 '22 10:09

Rauli Rajande