How to prevent text overflow and from clipping?

I need to use italic font inside a flexible frame, but the text is cropped due to overflow.

Conditions for my text:

  • Overflow should not be visible.
  • Text should not be cropped

This is my current situation (a small blue square indicates the size of the text box) :

enter image description here

And I want this:

enter image description here

How can this be achieved?

I have a hack from this SO post , but this creates a serious problem, which you can see in the snippet below.

My current code is 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>
Run code
+4
source share
1 answer

Try using the CSS property letter-spacing.

.

.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; */
}

letter-spacing . .

:


:

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

&nbsp; , :

  • : &#8201; ( &thinsp;)
  • : &#8202;
+1

Source: https://habr.com/ru/post/1649924/


All Articles