.text{
max-width: 200px;
border-bottom: 1px dotted;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
}
<span class="text">dsagjadsldsagjadsldsagjadsldsagjadsldsagjadsldsagjadsldsagjadsl</span>
When the text is very long, the bottom border is slightly wider than the text.
Is there any solution to make both strictly the same length?
I think it may be caused by not being able to accommodate the truncated first character.
2
Answers
enter image description here
It's seems like this
To ensure that the border length matches the text length exactly, you can make use of a container element with relative positioning. By adding a pseudo-element to the container with an absolute position, you can achieve the desired effect. Here’s an updated version of your code:
In this updated code, the
.container
element acts as a wrapper around the.text
element. The::after
pseudo-element is added to the container, and it creates a border that matches the width of the text, even when it’s truncated.This way, the border will always be the same length as the visible text, regardless of whether it’s truncated or not.