在HTML+CSS中,长字符串的自动换行问题,总是造成意想不到的麻烦。下面是一些相关的总结。
针对于div、p等块级元素:
1.对于正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行。
1
#wrap{white-space:normal; width:200px; }
3
<
div
id
=
"wrap"
>对于正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行。</
div
>
2.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word;或者word-break:break-all;实现强制断行
1
#wrap{word-break:break-all; width:200px;} 或者#wrap{word-wrap:break-word; width:200px;}
3
<
div
id
=
"wrap"
>wwwiteaocomwwwiteaocomww witeaocomiteao</
div
>
3.(Firefox浏览器)连续的英文字符和阿拉伯数字的断行,Firefox的所有版本的没有解决这个问题,我们只有让超出边界的字符隐藏或者,给容器添加滚动条
1
#wrap{word-break:break-all; width:200px;overflow:auto;}
3
<
div
id
=
"wrap"
>abcdefghijklmnabcdefghij klmnabcdefghijklmn111111 111</
div
>
对于表格table:
1. (IE浏览器)使用 table-layout:fixed;强制table的宽度,多余内容隐藏
1
<
table
style
=
"table-layout:fixed"
width
=
"200"
>
3
<
td
>abcdefghigklmnopqrstuvwx yz1234567890ssssssssssss ss</
td
>
2.(IE浏览器)使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break :break-all;或者word-wrap : break-word ;换行
1
<
table
width
=
"200"
style
=
"table-layout:fixed;"
>
3
<
td
width
=
"25%"
style
=
"word-break :break-all; "
>wwwiteaocomwwwiteaocomww witeaocomiteao</
td
>
4
<
td
style
=
"word-wrap :break-word ;"
>wwwiteaocomwwwiteaocomww witeaocomiteao</
td
>
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;无法起作用
1
<
table
style
=
"table-layout:fixed"
width
=
"200"
>
3
<
td
width
=
"25%"
style
=
"word-break :break-all; overflow:hidden; "
>abcdefghigklmnopqrstuvwx yz1234567890</
td
>
4
<
td
width
=
"75%"
style
=
"word-wrap :break-word; overflow:hidden; "
>abcdefghigklmnopqrstuvwx yz1234567890</
td
>
5.(Firefox浏览器)在td,th中嵌套div,p等采用上面提到的对付Firefox的方法运行代码框,最后,这种现象出现的几率很小,但是不能排除网友的恶搞。
04
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html;charset=gb2312"
/>
06
<
style
type
=
"text/css"
>
07
table,td,th,div {border:1px green solid;}
08
code {font-family:"Courier New", Courier, monospace;}
16
<
code
>Allwhite-space:normal;</
code
>
18
<
div
style
=
"white-space:normal;width:200px;"
>Wordwrap still occurs in a td elementthat has its WIDTH attribute set to a value smaller than theunwrapped content of the cell, even if the noWrap property is setto true. Therefore, the WIDTH attribute takes precedence over thenoWrap property in thisscenario</
div
>
21
<
code
>IE word-wrap : break-word ;</
code
>
23
<
div
style
=
"word-wrap :break-word ; width:200px;"
>abcdefghijklmnabcdefghij klmnabcdefghijklmn111111 111</
div
>
25
<
code
>IE word-break:break-all;</
code
>
27
<
div
style
=
"word-break:break-all;width:200px;"
>abcdefghijklmnabcdefghij klmnabcdefghijklmn111111 111</
div
>
29
<
code
>Firefox/ word-break:break-all;overflow:auto;</
code
>
31
<
div
style
=
"word-break:break-all;width:200px; overflow:auto;"
>abcdefghijklmnabcdefghij klmnabcdefghijklmn111111 111</
div
>
36
<
code
>table-layout:fixed;</
code
>
38
<
table
style
=
"table-layout:fixed"
width
=
"200"
>
40
<
td
>abcdefghigklmnopqrstuvwx yz1234567890ssssssssssss ss</
td
>
44
<
code
>table-layout:fixed; word-break :break-all; word-wrap : break-word;</
code
>
46
<
table
width
=
"200"
style
=
"table-layout:fixed;"
>
48
<
td
width
=
"25%"
style
=
"word-break :break-all; "
>abcdefghigklmnopqrstuvwx yz1234567890ssssssssssss ss</
td
>
49
<
td
style
=
"word-wrap :break-word ;"
>abcdefghigklmnopqrstuvwx yz1234567890ssssssssssss ss</
td
>
53
<
code
>FF table-layout:fixed;overflow:hidden;</
code
>
55
<
table
style
=
"table-layout:fixed"
width
=
"200"
>
57
<
td
width
=
"25%"
style
=
"word-break :break-all; overflow:hidden; "
>abcdefghigklmnopqrstuvwx yz1234567890</
td
>
58
<
td
width
=
"75%"
style
=
"word-wrap :break-word; overflow:hidden; "
>abcdefghigklmnopqrstuvwx yz1234567890</
td
>