Applying the ellipsis
Using Bootstrap:
If you are using Bootstrap, you can apply the text-truncate
class to add an ellipsis to any text, like so:
<span id="ellipsis-ex" class="d-inline-block text-truncate" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
Using plain CSS:
Else, you can define the appropriate class to generate the ellipsis as you've mentioned in the question:
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Result:
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<span class="text-truncate" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
Toggling the ellipsis
Using plain JS:
To toggle the class using pure JS, use
var element = document.querySelector("ellipsis-ex");
element.classList.toggle("text-truncate");
You can also use the classList.add("#ellipsis-ex")
and classList.remove("ellipsis-ex")
functions to specifically add or remove the class
Angular
Reading your question, it seems like you're working with Angular therefore you can use the built-in class
directive to toggle the class in the template itself.
<!-- toggleEllipses is a boolean indicating ellipsis status -->
<span id="ellipsis-ex" [class.text-truncate]="toggleEllipses" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
Result:
var element = document.querySelector("#ellipsis-ex");
function toggleEllipsis() {
element.classList.toggle("text-truncate");
}
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<span id="ellipsis-ex" class="text-truncate" style="max-width: 150px;" onclick="toggleEllipsis()">
Praeterea iter est quasdam res quas ex communi.
</span>