I would like to change the color of the text when the string is too long. If a string is too long, it will display ‘…’ at the end with the color red otherwise the color will be black. I am not sure if it is possible to achieve.
This is my css class
textStyle:{
whiteSpace: 'pre-line',
paddingTop:5,
overflow: 'hidden',
'text-overflow': 'ellipsis',
display: '-webkit-box',
'-webkit-line-clamp': 3,
'-webkit-box-orient': 'vertical'
}
My frontend
String testString = "This is a long long text, it will show three dot when it has more than three line..."
<div className={classes.textStyle}>{testString}</div>
What it does is when the text is more then 3 line, it will show ‘…’at the end like below
This is a long long text,
it will show three dot when
it has more than three line...
What I want is the change the whole text to font color to red when above case happen. However, above css will also change the color of the text when it only has one line
This is a long long text
2
Answers
It is not possible with css only. It does not know it the text overflows.
With JS maybe you can check if the inner element has less height than the outer
The outer element will stop to grow on 3rd line, but the inner one will get all the height of the text. So if
outer.offsetHeight < inner.offsetHeight
that means the text overflows. This needs to be checked on resize aswellSomething like this: