3

I have a long word in a table cell that messes up the table layout:

a.long.word.separated.by.dots

I would like the word to wrap like this word:

a-long-word-separated-by-dashes

How can I get this done, with CSS, without changing the HTML?

Simon
  • 321
  • 3
  • 11

4 Answers4

11

If using JS is okay, you can do this. If it is not, then this answer may still be useful for future readers.

var longWord = "a.long.word.separated.by.dots".replace(/\./g, '.<wbr>');

Reference on the wbr tag: http://www.w3.org/wiki/HTML/Elements/wbr

Alternatively, you could put a <wbr> after each period manually.

11684
  • 7,356
  • 12
  • 48
  • 71
1

You can do it in PHP, sorry for waking a dead question, but if you're populating an HTML table using a PHP loop you just need something like:

preg_replace('/\./', '.<wbr>', $string);

This takes 11684's answer and puts it into the requested context.

Community
  • 1
  • 1
Milhouse
  • 11
  • 2
0

<wbr> didn't work for me, but a zero-width space did. You can use:

var longWord = "a.long.word.separated.by.dots".replace(/\./g, '.&#x200b;');
Paul
  • 6,061
  • 6
  • 39
  • 70
  • A downside of using a zero-width space is that it will be copied to the clipboard if the user copies text from the Web page. This could cause a lot of confusion depending on what application the text ends up in. – kindall Oct 04 '17 at 22:23
  • @kindall you are right. Fortunately, that's not an issue in my case - the text is not selectable. – Paul Oct 04 '17 at 22:38
-3

You can use this CSS3 property:

table tr td {
    word-wrap: break-word;
}

Browser support: http://caniuse.com/#search=word-wrap

Simone
  • 20,302
  • 14
  • 79
  • 103