I need to use italic font inside a flex box, but texts get clipped because of overflow.
Conditions I want for my text:
This is my current situation (small blue box indicates text box size):
And I want this:
How can this be achieved?
I got an hack from this SO post but it makes a serious problem that you can see on the snippet below.
My current code here:
body {
font-family: sans-serif;
font-size: 30px;
}
.ellipsiswrap {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.italic {
font-style: italic;
}
.italicguard {
padding-right: 0.3em;
margin-right: -0.3em;
}
.usernamewrapper {
font-size: 1.2em;
overflow: hidden;
display: flex;
background-color: #303030;
color: #fff;
}
.opacity5 {
opacity: 0.5;
}
.screenname {
margin-left: 0.3em;
flex: 1;
}
.widthlimit150 {
width: 300px;
}
.widthlimit70 {
width: 140px;
}
.letterspacing {
letter-spacing: .12em
}
.wordspacing {
word-spacing: .12em
}
<p>Original:</p>
<div class="usernamewrapper">
<span class="ellipsiswrap italic">username W</span>
<span class="screenname ellipsiswrap opacity5 italic italicguard">@username</span>
</div>
<p>Hack:</p>
<div class="usernamewrapper italic italicguard">
<span class="ellipsiswrap italicguard">username W</span>
<span class="screenname ellipsiswrap opacity5 italicguard">@username</span>
</div>
<p>Behavior that must be kept:</p>
<div class="usernamewrapper italic widthlimit150">
<span class="ellipsiswrap">username W</span>
<span class="screenname ellipsiswrap opacity5">@username</span>
</div>
<div class="usernamewrapper italic widthlimit70">
<span class="ellipsiswrap">username W</span>
<span class="screenname ellipsiswrap opacity5">@username</span>
</div>
<p>Problem with the hack:</p>
<div class="usernamewrapper italic italicguard widthlimit70">
<span class="ellipsiswrap italicguard">username W</span>
<span class="screenname ellipsiswrap opacity5 italicguard">@username</span>
</div>
<p>letter-spacing from Michael_B:</p>
<div class="usernamewrapper">
<span class="ellipsiswrap italic letterspacing">username W</span>
<span class="screenname ellipsiswrap opacity5 italic italicguard">@username</span>
</div>
<p>word-spacing from Michael_B:</p>
<div class="usernamewrapper">
<span class="ellipsiswrap italic wordspacing">username W</span>
<span class="screenname ellipsiswrap opacity5 italic italicguard">@username</span>
</div>
Try using the CSS letter-spacing
property.
It essentially adds padding before and after each letter.
.ellipsiswrap {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
letter-spacing: .12em; /* just enough to clear the last letter;
even .2em doesn't look bad; */
}
I often use letter-spacing
to improve readability. In this case, it may help prevent the cut-off.
Reference:
letter-spacing
and word-spacing
propertiesAnother potential option is to add a trailing space to each username:
<span class="ellipsiswrap italic">username W </span>
If
is too wide, there are narrower options:
 
(also  
) 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With