In fact, you can set the text-overflow value to a string, for example:
.myclass ul li{ text-overflow: '...'; }
But:
In a draft, he even says:
Note. The <string> value and the 2-digit value syntax "{1,2}" and functionality are at risk.
Basically, do not use a string value.
Because of this, I would just use a class if the overflow should not be dynamic, or JS if it is.
Js solution
var p = document.querySelector(".js-overflow"); if (p.scrollWidth > p.offsetWidth) p.classList.add("has-overflow"); while (p.scrollWidth > p.offsetWidth) { p.innerHTML = p.innerHTML.slice(0, -1); }
p { width: 200px; border: 1px solid; padding: 2px 5px; /* BOTH of the following are required for text-overflow */ white-space: nowrap; overflow: hidden; } .has-overflow:after { content: "..." }
<p class="js-overflow"> Testing overflow yaya look at me ! </p>
The JS solution is actually very simple.
- Make sure that the scroll width of the items is larger than the actual width (which means overflow)
- Loop by removing the last character while the scroll width with the contents of the
after pseudo-elements is less than the width.
Jacob gray
source share