The layout of text will have some format requirements depending on the language. For example, punctuation marks such as commas and semicolons in simplified Chinese will not appear at the beginning of a line. For English, a complete word will not be displayed on two lines. The browser will display the text based on principles like this. However, because the web page has width limitations, continuous and long letters, numbers or punctuation marks exceed the limit of the width of the area where it is located, which affects the page vision, as shown in Example 1. This problem is particularly prominent when displaying user input information. Here we are talking about how to solve this problem.
In the CSS3 draft, two new properties word-wrap and word-break are added to the processing of text to solve this problem:
The following are the support situations of common browsers:
| IE6/7[1] | Firefox2/3[2] | Opera3+ | Safari9.5+/Chrome | |
|---|---|---|---|---|
| {word-wrap:break-word;} | The width of the td element needs to be set See Example 4 and Example 5 | Not supported | Not supported | Not supported td elements See Example 4 and Example 5 |
| {word-break:break-all;} | Continuous symbols are not supported See Example 3 | Not supported | Not supported | support |
<meta content="IE=7" http-equiv="X-UA-Compatible" /> at the header to make IE8 interpret the page according to IE7.Since {word-break:break-all;} causes severe readability of English and numericals and fails to wrap consecutive symbols, {word-wrap:break-word;} is a relatively good choice.
However, in the face of such poor support from the browser, we cannot solve this problem without using JavaScript. That is, when the browser does not support the CSS solution, insert the "8203" characters in the appropriate position of the continuous string (of course you can also use <wbr /> and "shy;. For the support of these three characters in the browser, please see ppk's "The wbr tag"). These characters will not be displayed in the browser, but will cause long strings to be wrapped. The specific implementation code is as follows:
function fnBreakWordAll(o){var o = o || {},
iWord = o.word || 13,
iRe = o.re || '[a-zA-Z0-9]',
bAll = o.all || false,
sClassName = o.className || 'word-break-all',
aEls = o.els || (function(){
var aEls = [],
aAllEls = document.getElementsByTagName('*'),
re = new RegExp('(?:^|//s+)' + sClassName + '(?://s+|$)');
for(var i =0,iLen = aAllEls.length; i < iLen ; ++i){
if(re.test(aAllEls[i].className)){
aEls[aEls.length] = aAllEls[i];
}
}
return aEls;
})() || [],
fnBreakWord = function(oEl){
// Modify based on http://www.hedgerwow.com/360/dhtml/css-word-break.html
if(!oEl || oEl.nodeType !== 1){
return false;
}else if(oEl.currentStyle && typeof oEl.currentStyle.wordWrap==='string'){
breakWord = function(oEl){
oEl.runtimeStyle.wordWrap = 'break-word';
return true;
}
return breakWord(oEl);
}else if(document.createTreeWalker){
var trim = function (str) {
str = str.replace(/^/s/s*/, '');
var ws = //s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
breakWord = function(oEl){
var dWalker=document.createTreeWalker(oEl,NodeFilter.SHOW_TEXT,null,false);
var node,s,c = String.fromCharCode('8203'),
//re = /([a-zA-Z0-9]{0,13})/;
re = new RegExp('('+ iRe +'{0,' + iWord + '})');
while (dWalker.nextNode()){
node = dWalker.currentNode;
s = trim(node.nodeValue).split(re).join(c);
node.nodeValue = s;
}
return true;
}
return breakWord(oEl);
}
};
for(var i=0,n=aEls.length; i<n; ++i){
var sUa = navigator.userAgent,
sTn = aEls[i].tagName.toLowerCase();
if((/Opera/).test(sUa) || (/Firefox/).test(sUa) || ((/KHTML/).test(sUa) &&
(sTn === 'td' || sTn === 'th')) || bAll){
fnBreakWord(aEls[i]);
}
}
}
For specific applications, please see the demo example
The fnWordBreakAll function provides some customized parameters, and the usage methods and parameters are as follows:
fnWordBreakAll({word:15,re:'[//w]',all:true});| parameter | value | illustrate |
|---|---|---|
| Word | Positive integer, default is 13 | Words within this word count will not be inserted into /u8203. There are not many words with more than 13 letters in my impression, which can ensure that most words will not be broken |
| re | Regular expression, default [a-zA-Z0-9] | Regular expression of a word to determine which characters a word is composed of, pay attention to the escape of / |
| all | Boolean value, default false | Determine whether it is executed in all browsers, by default in Opera and Firefox, and when the class application is executed on Safari in th or td, this is mainly used when .word-break-all is not defined, it will add styles to IE. |
| className | Legal class name, default word-break-all | The corresponding attribute name of the element of the execution function |
The core part of this function was modified from Hedger Wang. A JavaScript solution "Cross Browser Word Breaker" was compiled. It uses document.createTreeWalker and split methods to add "#8203" to each character in non-IE browsers. It is basically no problem when it is used in pure Chinese text, but if you observe the examples he gives, you will find that when there are English or numbers in the text, although it solves the line break problem, it makes the text difficult to read and increases the spacing between characters, so I made the above improvements based on this.