Wrap text in td without breaking the word

谁说我不能喝 提交于 2019-12-06 07:46:09

I can't think of a CSS-only solution. But using JavaScript you could add the word break opportunity tag <wbr>. This tells the browser - except for Internet Explorer - where it can insert breaks:

JavaScript (using jQuery)

$('#element').html( $('#element').html().replace(/\//g, '/<wbr>') );

Demo

http://jsfiddle.net/EkfUR/

As of your edit

You edited your question while I was writing the fiddle. You can of course use another character than / used in the expression to be replaced with the <wbr>-tag.

Milche Patern

I see nothing in .css to break text part on specific point ( break row after letter 't' for example.

Search for 'word-break'

caniuse word-break

p {
    -ms-word-break: break-all;
    word-break: break-all;

     // Non standard for webkit
     word-break: break-word;

     -webkit-hyphens: auto;
     -moz-hyphens: auto;
     hyphens: auto;
 }

There is a nice topic relevant to your situation : how-to-prevent-long-words-from-breaking-my-div invoking possibility to split long words by inserting soft hyphen (­) with a regular expression.

You can try doing it on javascript.

1) Split the string in "/";

2) Append substrings from start to end, while the resulting line is smaller than the width of the TD;

3) If it's bigger, don't append the last piece, and place it in the next line (as in, put a <br /> before it);

4) Do this until your split array is empty, which means you have your string broken in lines like you want.

ok, maybe this is the worst approach you'll see, but it does the trick... split the string into span and then use an :after whith a white space like this:

span:after{
 content:' ';
    font-size:0;
}

example here: http://jsfiddle.net/pavloschris/b7qeD/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!