css中怎么控制文字自动换行

今天就跟大家聊聊有关css中怎么控制文字自动换行,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

对于div,p等块级元素

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

html

正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义

css

CSS Code复制内容到剪贴板

  1. #wrap{whitewhite-space:normalwidth:200px; }   

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

CSS Code复制内容到剪贴板

  1. #wrap{word-break:break-allwidth:200px;}   

或者

CSS Code复制内容到剪贴板

  1. #wrap{word-wrap:break-word; width:200px;}   

  2.   

  3. abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111   

效果:可以实现换行

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

CSS Code复制内容到剪贴板

  1. #wrap  

  2.   

  3. {word-break:break-allwidth:200pxoverflow:auto;}   

  4.   

  5. abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111   

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

对于table

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

XML/HTML Code复制内容到剪贴板

  1. <table style="table-layout:fixed" width="200">  

  2. <tr>  

  3. <td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss   

  4. </td>  

  5. </tr>  

  6. </table>  

效果:隐藏多余内容

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

XML/HTML Code复制内容到剪贴板

  1. <table width="200" style="table-layout:fixed;">    

  2.     <tr>    

  3.         <td width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz 1234567890      

  4.         </td>    

  5.         <td style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz 1234567890   

  6.         </td>    

  7.     </tr>    

  8. </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;无法起作用

XML/HTML Code复制内容到剪贴板

  1. <table style="table-layout:fixed" width="200">  

  2. <tr>  

  3. <td width="25%"  style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>  

  4. <td width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>  

  5. </tr>  

  6. </table>  

效果:隐藏多于内容

5.(Firefox浏览器) 在td,th中嵌套div,p等采用上面提到的对付Firefox的方法
运行代码框
最后,这种现象出现的几率很小,但是不能排除网友的恶搞。如果

有什么问题请到在下面留言

下面是提到的例子的效果

XML/HTML Code复制内容到剪贴板

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

  2. <html xmlns="http://www.w3.org/1999/xhtml">  

  3. <head>  

  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  

  5. <title>字符换行   

  6.     

  7. </title>  

  8. <style type="text/css">  

  9. table,td,th,div { border:1px green solid;}   

  10. code { font-family:"Courier New", Courier, monospace;}   

  11.     

  12. </style>  

  13. </head>  

  14. <body>  

  15. <h2><code>div</code></h2>  

  16. <h2><code>All white-space:normal;</code></h2>  

  17. <div style="white-space:normal; width:200px;">Wordwrap still occurs in a td element that    

  18. has its WIDTH attribute set to a value smaller than the unwrapped content of the cell,    

  19. even if the noWrap property is set to true. Therefore, the WIDTH attribute takes    

  20. precedence over the noWrap property in this scenario</div>  

  21.     

  22. <h2><code>IE  word-wrap : break-word ;</code></h2>  

  23. <div style="word-wrap : break-word ; width:200px;">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>  

  24. <h2><code>IE  word-break:break-all;</code></h2>  

  25. <div style="word-break:break-all;width:200px;">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>  

  26.     

  27. <h2><code>Firefox/ word-break:break-all; overflow:auto;</code></h2>  

  28. <div style="word-break:break-all; width:200px; overflow:auto;">abcdefghijklmnabcdefghijklmnabcdefghijkl   

  29. mn111111111</div>  

  30. <h2><code>table</code></h2>  

  31. <h2><code>table-layout:fixed;</code></h2>  

  32. <table style="table-layout:fixed" width="200">  

  33. <tr>  

  34. <td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>  

  35. </tr>  

  36. </table>  

  37. <h2><code>table-layout:fixed; word-break : break-all; word-wrap : break-word ;</code></h2>  

  38. <table width="200" style="table-layout:fixed;">  

  39. <tr>  

  40. <td width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>  

  41. <td style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>  

  42. </tr>  

  43. </table>  

  44. <h2><code>FF  table-layout:fixed; overflow:hidden;</code></h2>  

  45. <table style="table-layout:fixed" width="200">  

  46. <tr>  

  47. <td width="25%"  style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>  

  48. <td width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>  

  49. </tr>  

  50. </table>  

  51. </body>  

  52. </html>  

看完上述内容,你们对css中怎么控制文字自动换行有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。