I have a set of links that need to be truncated. I know that text.overflow: elipsis
on anchors can be troublesome, but here’s my question:
How can I make anchor text-decoration: underline
follow the width of the truncated anchor?
As you can see, the three dots are positioned outside the link underline.
I also tried to add the truncation to the inner span of each link, but it makes the link’s underline disappear.
How can I solve this?
a {
text-decoration: underline;
width: 150px;
display: inline-block;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<ul>
<li>
<a href="#"><span class="link-text">Menu item</span></a>
</li>
<li>
<a href="#"><span class="link-text">Very long menu item that needs to be truncated</span></a>
</li>
<li>
<a href="#"><span class="link-text">Menu item</span></a>
</li>
</ul>
2
Answers
A pseudo-element on the
span
, suitable positioned, would not be a complete replacement fortext-decoration
but it would be a functional equivalent.You can simply add a
border-bottom
to the anchor. However you should usemax-width
instead ofwidth
so the border does nto streatch to the full 150px in every case.