css控制文字自动换行的如何实现方法

太阳渐渐往下落,它的脸涨得越来越红,红的像个大火球,把身边的云染成五颜六色。慢慢地它走到西山背后,把美丽的霞光留在遥远的天边。我们都看得目瞪口呆。我的心里在想:晚霞真美!

自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法

对于div,p等块级元素

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

html

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

css

CSS Code复制内容到剪贴板
  1. #wrap{whitewhite-space:normal;width:200px;}
1.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行
CSS Code复制内容到剪贴板
  1. #wrap{word-break:break-all;width:200px;}
或者
CSS Code复制内容到剪贴板
  1. #wrap{word-wrap:break-word;width:200px;}
  2. abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111

效果:可以实现换行

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

CSS Code复制内容到剪贴板
  1. #wrap
  2. {word-break:break-all;width:200px;overflow:auto;}
  3. abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111

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

对于table

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

XML/HTML Code复制内容到剪贴板
  1. <tablestyle="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. <tablewidth="200"style="table-layout:fixed;">
  2. <tr>
  3. <tdwidth="25%"style="word-break:break-all;">abcdefghigklmnopqrstuvwxyz1234567890
  4. </td>
  5. <tdstyle="word-wrap:break-word;">abcdefghigklmnopqrstuvwxyz1234567890
  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. <tablestyle="table-layout:fixed"width="200">
  2. <tr>
  3. <tdwidth="25%"style="word-break:break-all;overflow:hidden;">abcdefghigklmnopqrstuvwxyz1234567890</td>
  4. <tdwidth="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. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
  5. <title>字符换行
  6. </title>
  7. <styletype="text/css">
  8. table,td,th,div{border:1pxgreensolid;}
  9. code{font-family:"CourierNew",Courier,monospace;}
  10. </style>
  11. </head>
  12. <body>
  13. <h1><code>div</code></h1>
  14. <h1><code>Allwhite-space:normal;</code></h1>
  15. <divstyle="white-space:normal;width:200px;">Wordwrapstilloccursinatdelementthat
  16. hasitsWIDTHattributesettoavaluesmallerthantheunwrappedcontentofthecell,
  17. evenifthenoWrappropertyissettotrue.Therefore,theWIDTHattributetakes
  18. precedenceoverthenoWrappropertyinthisscenario</div>
  19. <h1><code>IEword-wrap:break-word;</code></h1>
  20. <divstyle="word-wrap:break-word;width:200px;">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>
  21. <h1><code>IEword-break:break-all;</code></h1>
  22. <divstyle="word-break:break-all;width:200px;">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>
  23. <h1><code>Firefox/word-break:break-all;overflow:auto;</code></h1>
  24. <divstyle="word-break:break-all;width:200px;overflow:auto;">abcdefghijklmnabcdefghijklmnabcdefghijkl
  25. mn111111111</div>
  26. <h1><code>table</code></h1>
  27. <h1><code>table-layout:fixed;</code></h1>
  28. <tablestyle="table-layout:fixed"width="200">
  29. <tr>
  30. <td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>
  31. </tr>
  32. </table>
  33. <h1><code>table-layout:fixed;word-break:break-all;word-wrap:break-word;</code></h1>
  34. <tablewidth="200"style="table-layout:fixed;">
  35. <tr>
  36. <tdwidth="25%"style="word-break:break-all;">abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>
  37. <tdstyle="word-wrap:break-word;">abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss</td>
  38. </tr>
  39. </table>
  40. <h1><code>FFtable-layout:fixed;overflow:hidden;</code></h1>
  41. <tablestyle="table-layout:fixed"width="200">
  42. <tr>
  43. <tdwidth="25%"style="word-break:break-all;overflow:hidden;">abcdefghigklmnopqrstuvwxyz1234567890</td>
  44. <tdwidth="75%"style="word-wrap:break-word;overflow:hidden;">abcdefghigklmnopqrstuvwxyz1234567890</td>
  45. </tr>
  46. </table>
  47. </body>
  48. </html>

以上就是小编为大家带来的css控制文字自动换行的实现方法全部内容了,希望大家多多支持~

您可能有感兴趣的文章
css让页脚保持在底部位置的四种方案

CSS如何使用Flex和Grid布局如何实现3D骰子

Flex布局史上最简单使用语法教程

新的CSS 伪类函数 :is() 和 :where()示例详解

纯CSS打字动画的如何实现示例