Solution to the problem of English letters not wrapping in Firefox

Solution to the problem of English letters not wrapping in Firefox

The layout of text has some formatting requirements depending on the language. For example, in simplified Chinese, punctuation marks such as commas and semicolons 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 text based on similar principles. However, due to the width limitation of web pages, continuous extra-long letters, numbers or punctuation marks exceed the width limit of the area where they are located, thus affecting the visual effect of the page, as shown in Example 1. This problem is particularly prominent when displaying user input information, and here we will talk about how to solve this problem.

In the CSS3 draft, two new properties, word-wrap and word-break, are added to the text processing to solve this problem:

  1. {word-wrap:break-word;} : allows line breaks within words, in accordance with the text rules of Asian and non-Asian languages, and allows non-Asian language text lines to be broken within any word
  2. {word-break:break-all;} : The content will wrap within the boundary, and word-breaks will also occur if necessary.

The following are the common browser support:

IE6/7 [1] Firefox 2/3 [2] Opera3+ Safari9.5+/Chrome
{word-wrap:break-word;} The td element needs to set its width, see Example 4 and Example 5 Not supported Not supported td element is not supported. See Example 4 and Example 5
{word-break:break-all;} Continuous symbols are not supported. See Example 3. Not supported Not supported support
  • [1] From MSDN's CSS Compatibility and Internet Explorer, we can see that IE8 supports the word-wrap and word-break properties just like IE6 and IE7. However, when I tested IE8beta2, I found that it does not support these two properties. Since IE8 is still in the beta stage and I believe IE8 will eventually support them, it is a good idea to declare <meta content="IE=7" http-equiv="X-UA-Compatible" /> in the page header to make IE8 interpret the page according to IE7.
  • [2] Firefox 3.1 will support the word-wrap attribute

Since {word-break:break-all;} seriously reduces the readability of English and numbers and fails to wrap consecutive symbols, {word-wrap:break-word;} is a relatively good choice.

But faced with such poor browser support, we have to resort to JavaScript to solve this problem, that is, when the browser does not support the CSS solution, insert &#8203; characters at the appropriate position of the continuous string (of course, you can also use <wbr /> and &shy;, for the support of these three characters in browsers, please see ppk's "The wbr tag"). These characters will not be displayed when written in the browser, but will make the long string wrap. 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){
// 基于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]);
}
}
}

Please see the demonstration example for specific application

The fnWordBreakAll function provides some custom parameters. The usage and parameter meanings are as follows:

 fnWordBreakAll({word:15,re:'[\\w]',all:true});
parameter value illustrate
word Positive integer, default is 13 Words within this number will not be inserted. \u8203, I don't think there are many words with more than 13 letters, so this ensures that most words will not be broken up.
re Regular expression, default [a-zA-Z0-9] The regular expression of a word is used to determine which characters a word is composed of. Note the escape of \
all Boolean value, default false Determines whether to execute in all browsers. By default, it is executed in Opera and Firefox, and in Safari when the class is applied to th or td. This is mainly used when .word-break-all is not defined, which will add styles to IE
className Legal class name, default is word-break-all The attribute name corresponding to the element that executes the function

The core part of this function is modified from Hedger Wang's JavaScript solution "Cross Browser Word Breaker", which uses document.createTreeWalker and split method to add &#8203; between each character in non-IE browsers. It can be used for pure Chinese text without any problems, but if you look closely at the examples he gave, 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 on this basis.

<<:  Solution to the failure of docker windows10 shared directory mounting

>>:  Explain TypeScript enumeration types in detail

Recommend

How to deploy Solidity smart contracts using ethers.js

If you have developed DApps on Ethereum, you may ...

JavaScript canvas to achieve raindrop effect

This article example shares the specific code for...

Code to display the contents of a txt book on a web page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

CSS style reset and clear (to make different browsers display the same effect)

In order to make the page display consistent betwe...

The most comprehensive collection of front-end interview questions

HTML+CSS 1. Understanding and knowledge of WEB st...

Solution to nginx not jumping to the upstream address

Preface Today I encountered a very strange proble...

Detailed explanation of the basic usage of the Linux debugger GDB

Table of contents 1. Overview 2. gdb debugging 2....

Linux CentOS6.5 yum install mysql5.6

This article shares the simple process of install...

Vue uses ECharts to implement line charts and pie charts

When developing a backend management project, it ...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

How to deploy nodejs service using Dockerfile

Initialize Dockerfile Assuming our project is nam...

MySQL 8.0.22 decompression version installation tutorial (for beginners only)

Table of contents 1. Resource download 2. Unzip t...

Three methods to modify the hostname of Centos7

Method 1: hostnamectl modification Step 1 Check t...

CSS realizes the mask effect when the mouse moves to the image

1. Put the mask layer HTML code and the picture i...