Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text Overflow - Prevent from clipping letters in half

Tags:

css

For my situation, I have a fixed width in which I can show a name. If the name is too long, then I need to clip/hide the name.

The issue I am having is the last letter is being cut in half, whereas I would only like that last whole letter to show.

In the example below for "HelloWorld!", the 'W' is cut in half. Instead I need it to ignore that partial letter and just show "Hello". Is this even possible with just CSS?

.test {
  white-space: nowrap;
  width: 44px;
  overflow: hidden;
  text-overflow: clip;
  border: 1px solid #000000;
}
<div class="test">HelloWorld!</div>
like image 908
bsparacino Avatar asked Aug 30 '18 20:08

bsparacino


2 Answers

.test {
  width: 44px;
  height: 18px;
  word-break: break-all;
  overflow: hidden;
  border: 1px solid #000000;
}
<div class="test">HelloWorld!</div>
like image 161
Roman Maksimov Avatar answered Oct 23 '22 05:10

Roman Maksimov


You can consider ellipsis instead of clip:

.test {
  white-space: nowrap;
  width: 44px;
  overflow: hidden;
  text-overflow: clip;
  border: 1px solid #000000;
}

.test2 {
  width: 44px;
  overflow: hidden;
  border: 1px solid #000000;
}

.test2>span {
  display: block;
  width: calc(100% + 10px);
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
<div class="test">HelloWorld!</div>

<div class="test2"><span>HelloWorld!</span></div>
<div class="test2"><span>Sometext</span></div>
<div class="test2"><span>Myname</span></div>

UPDATE

You can also consider word-break but in this case you need to have a fixed height also:

.test {
  width: 44px;
  height:20px;
  overflow: hidden;
  word-break:break-all;
  border: 1px solid #000000;
}
<div class="test">HelloWorld!</div>
<div class="test">Myname</div>
<div class="test">sometext!</div>
like image 37
Temani Afif Avatar answered Oct 23 '22 03:10

Temani Afif