不同浏览器对于换行的处理

简介: 在一个容器中,如果设定了宽度,一般来说自动换行都是比较正常的,但是如果遇到了连续的英文字符,这个问题就会让人头疼。这不,我们部门的用户在测试的时候输入连续的字符,就出现了容器被撑大而样式变形的情况发生,怎么解决这个问题呢?  正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-spac...

在一个容器中,如果设定了宽度,一般来说自动换行都是比较正常的,但是如果遇到了连续的英文字符,这个问题就会让人头疼。这不,我们部门的用户在测试的时候输入连续的字符,就出现了容器被撑大而样式变形的情况发生,怎么解决这个问题呢?

 正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行,如下:

html

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
<div id="wrap">正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义</div>

css

#wrap { white-space : normal ;  width : 200px ;   }


1.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行

#wrap{word-break:break-all; width:200px;}  或者  #wrap{word-wrap:break-word; width:200px;}

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div> 效果:可以实现换行

2.(Firefox浏览器)连续的英文字符和阿拉伯数字的断行,Firefox的所有版本的没有解决这个问题,我们只有让超出边界的字符隐藏或者,给容器添加滚动条

#wrap{word-break:break-all; width:200px; overflow:auto;}

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>

效果:容器正常,内容隐藏

对于table

1. (IE浏览器)使用 table-layout:fixed;强制table的宽度,多余内容隐藏

<table style="table-layout:fixed" width="200">
<tr>
<td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss
</td>
</tr>
</table>

效果:隐藏多余内容

2.(IE浏览器)使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break : break-all;或者word-wrap : break-word ;换行

<table width="200" style="table-layout:fixed;">
<tr>
<td width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
<td style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
</tr>
</table>

效果:可以换行

3. (IE浏览器)在td,th中嵌套div,p等采用上面提到的div,p的换行方法

4.(Firefox浏览器)使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break : break-all;或者word-wrap : break-word ;换行,使用overflow:hidden;隐藏超出内容,这里overflow:auto;无法起作用

<table style="table-layout:fixed" width="200">
<tr>
<td width="25%" style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
<td width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
</tr>
</table>

效果:隐藏多于内容

5.(Firefox浏览器) 在td,th中嵌套div,p等采用上面提到的对付Firefox的方法

下面是综合代码

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"   />
< title > 字符换行 </ title >
< style  type ="text/css" >
table,td,th,div 
{  border : 1px green solid ; }
code 
{  font-family : "Courier New", Courier, monospace ; }
</ style >
</ head >
< body >
< h1 >< code > div </ code ></ h1 >
< h1 >< code > All white-space:normal; </ code ></ h1 >
< div  style ="white-space:normal; width:200px;" > Wordwrap still occurs in a td element that has its WIDTH attribute set to a value smaller than the unwrapped content of the cell, even if the noWrap property is set to true. Therefore, the WIDTH attribute takes precedence over the noWrap property in this scenario </ div >

< h1 >< code > IE \ word-wrap : break-word ; </ code ></ h1 >
< div  style ="word-wrap : break-word ; width:200px;" > abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111 </ div >
< h1 >< code > IE \ word-break:break-all; </ code ></ h1 >
< div  style ="word-break:break-all;width:200px;" > abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111 </ div >

< h1 >< code > Firefox/ word-break:break-all; overflow:auto; </ code ></ h1 >
< div  style ="word-break:break-all; width:200px; overflow:auto;" > abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111 </ div >
< h1 >< code > table </ code ></ h1 >
< h1 >< code > table-layout:fixed; </ code ></ h1 >
< table  style ="table-layout:fixed"  width ="200" >
< tr >
< td > abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss </ td >
</ tr >
</ table >
< h1 >< code > table-layout:fixed; word-break : break-all; word-wrap : break-word ; </ code ></ h1 >
< table  width ="200"  style ="table-layout:fixed;" >
< tr >
< td  width ="25%"  style ="word-break : break-all; " > abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss </ td >
< td  style ="word-wrap : break-word ;" > abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss </ td >
</ tr >
</ table >
< h1 >< code > FF \ table-layout:fixed; overflow:hidden; </ code ></ h1 >
< table  style ="table-layout:fixed"  width ="200" >
< tr >
< td  width ="25%"  style ="word-break : break-all; overflow:hidden; " > abcdefghigklmnopqrstuvwxyz1234567890 </ td >
< td  width ="75%"  style ="word-wrap : break-word; overflow:hidden; " > abcdefghigklmnopqrstuvwxyz1234567890 </ td >
</ tr >
</ table >
</ body >
</ html >

 

 

 

 

相关文章
|
12月前
|
JavaScript 前端开发
浏览器行为
浏览器行为
52 0
|
Web App开发 前端开发 安全
一文带你详细了解浏览器安全
1.同源策略 浏览器的同源策略,限制了来自不同源的“document”或脚本,对当前“document”读取或设置某些属性。 为了不让浏览器的页面行为发生混乱,浏览器提出了“Origin”(源)这一概念,来自不同Origin的对象无法互相干扰。 对于JavaScript来说,以下情况被认为是同源与不同源的:
91 3
一文带你详细了解浏览器安全
|
存储 Web App开发 缓存
浏览器相关总结
浏览器相关总结
|
Web App开发 缓存 前端开发
浏览器端Less
浏览器端Less
|
Web App开发
浏览器突然好用多了。。。
我一向不掩饰对对浏览器插件的推崇。 首先,浏览器是日常工作中频繁接触的工具,借助浏览器这个平台,让插件触手可及,使用更加方便。 其次,插件普遍在几十KB或者上百KB,对比于动辄几十上百MB的桌面应用要轻量很多。
浏览器突然好用多了。。。
|
Web App开发 移动开发 测试技术
IE9是最佳浏览器?
据CNET科技资讯网11月10日国际报道,一开始只是个意外—一个专为宣传用的博客贴出了微软最新版的Internet Explorer 9 (IE9)。但不到一个星期内,错误的信息就马上传遍了整个软件市场。
986 0
|
Web App开发 安全 前端开发
|
Web App开发 应用服务中间件 nginx
|
安全 JavaScript 应用服务中间件