1

I have a module in the form:

.container {
  width: 250px;
  height: 50px;
  background: #fffff3;
}
.truncate {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<div class="container">
  <p class="truncate">
    <span>Some loooooooooooong name</span>
    :
    <span>1.7m</span>
    m Tall
  </p>
</div>

Which should render as:

Some customer name: 1.7m Tall

I want this to truncate so that if the name is too long it will look like:

Some long name...: 1.7m Tall

I've tried using the above truncate class but it appears like this:

Some loooooooooooong name...

With the users height off the screen. What can I do to rectify this?

Stickers
  • 75,527
  • 23
  • 147
  • 186
Eduardo
  • 6,900
  • 17
  • 77
  • 121

2 Answers2

3

You need to set ellipsis for the name element, to make it work you need also a fixed width and because it's a <span> you also need to change the display:

.container {
  width: 250px;
  height: 50px;
  background: #fffff3;
}
.truncate {
  display: inline-block;
  vertical-align:middle;
  width: 120px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<div class="container">
  <p>
    <span class="truncate">Some Looooongggg nameee</span>
    :
    <span>1.7 </span>
    M Tall
  </p>
</div>
DaniP
  • 37,813
  • 8
  • 65
  • 74
2

Try using flexbox layout, and set ellipsis on the first span only. It also supports dynamic length of the spans and plain text.

.truncate {
  display: flex;
  align-items: center; /* vertically center text */
  white-space: nowrap;
  width: 250px;
  height: 50px;
  padding: 0 8px 0 4px;
  background: gold;
}
.truncate span {
  min-width: 0;
  padding: 0 4px;
}
.truncate span:first-child {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  flex: 1; /* to take as much space as available */
}
<p class="truncate">
  <span>Looooooooooooooooooooooong name</span> :
  <span>1.7m</span> M Tall
</p>
Stickers
  • 75,527
  • 23
  • 147
  • 186