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.
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.
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.
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.
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>
.demo {
border: 1px solid red;
white-space: nowrap;
max-width: 100px;
overflow: hidden;
direction: rtl;
}
<p class="demo">hello this is a string</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With