I have a sidebar containing a list of categories, each of them followed by a badge with a number of unread articles in it. Since the categories are user-defined, they can occasionally be longer than the width of the sidebar and thus need to be ellipsized.
Now, my code ellipsizes the items but does so for the whole row, including the badge, which I would like to keep visible. I also want to have the badge just next to the label, not floated to the right.
ul {
width: 6em;
background: pink;
padding: 0.5em;
list-style: none;
}
li {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.badge {
color: gray;
}
<ul>
<li>Short <span class="badge">5</span></li>
<li>Much longer item <span class="badge">13</span></li>
<li>Another <span class="badge">2</span></li>
</ul>
I have come up with a flexbox-based solution but it requires an extra element. Is there a better way to do this?