Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a text from overflowing AND from being clipped?

Tags:

html

css

I need to use italic font inside a flex box, but texts get clipped because of overflow.

Conditions I want for my text:

  1. Overflow must not be visible
  2. Text must not be clipped

This is my current situation (small blue box indicates text box size):

enter image description here

And I want this:

enter image description here

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>
like image 918
Kagami Sascha Rosylight Avatar asked Oct 30 '22 22:10

Kagami Sascha Rosylight


1 Answers

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:

  • 16.4 Letter and word spacing: the letter-spacing and word-spacing properties

Another potential option is to add a trailing space to each username:

<span class="ellipsiswrap italic">username W&nbsp;</span>

If &nbsp; is too wide, there are narrower options:

  • Thin space: &#8201; (also &thinsp;)
  • Hair space: &#8202;
like image 168
Michael Benjamin Avatar answered Nov 15 '22 06:11

Michael Benjamin