From a previous question … Pseudo element :after and overflow hidden … I’ve been given the following code, which displays a * after a required form label, even if the form label has an ellipsis
It works perfectly when an ellipsis is displayed
BUT when no ellipsis is required, the positioning of the * is slightly off
Any fix for this? thx
label {
position: relative;
display: inline-block;
max-width: 240px;
}
label.required:after {
content: '*';
color: red;
position: absolute;
right: 0;
}
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div>
<label class="required ellipsis">long text works really really very really well</label>
</div>
<div>
<label class="required ellipsis">short text * positioning is off</label>
</div>
2
Answers
If you’re fine with moving the * slightly to the right in both cases, you could add a span around your text and add the ellipsis class to it. This will allow you to move the * to the right without it being affected by
overflow: hidden;
Like so:
No need to use
position: absolute;
.Wrap the text in a
<span>
and usedisplay: flex;
: