0

Is there a way to truncate a string to fit certain box size AND only keep full words?

CSS solution truncates to box size, but breaks words:

.truncate {

  width: 250px;

  white-space: nowrap;

  overflow: hidden; 

  text-overflow: ellipsis; 

}

While php truncate cuts the string to number of characters, but not to box length. Eg a string with many i's will take much less space then a string with many M's.

Is there a way to have best of both worlds?

skyisred
  • 6,855
  • 6
  • 37
  • 54
  • You should set the font size in pixels then divide the total width of your box in pixels on that font size to get the number of characters that should be in every line. – SaidbakR Nov 28 '14 at 23:01

1 Answers1

0

I don't think it's possible with CSS. There's a jQuery plugin I've used to do exactly this, http://dotdotdot.frebsite.nl/

Hope it helps, Gareth

Edit: Just found this on a duplicate post, https://stackoverflow.com/a/13015271/1692802 explains that there was a way with CSS but it was removed from the standards and not supported by many browsers text-overflow:ellipsis-word;. Also, someone mentioned the above plugin on there too!

Community
  • 1
  • 1
Gareth Hopkins
  • 354
  • 1
  • 11