我一直在使用word-wrap: break-word在div和span中对文本进行换行。然而,它似乎在表格单元格中不起作用。我有一个设置为width:100%的表,有一行和两列。列中的文本虽然使用了上面的换行样式,但不能自动换行。它会导致文本超出单元格的边界。这发生在Firefox,谷歌Chrome和Internet Explorer上。

下面是源代码的样子:

td { 边框:1px实体; L} <table style="width: 100%;"> < tr > < td > < span style=" font - family:宋体;"> Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong词 < / div > 道明> < / <span style=" font - family:宋体;">短字< / span > < / td > < / tr > 表> < /

上面的长单词比我的页面的边界大,但它没有与上面的HTML中断。我尝试了以下添加文本换行:抑制和文本换行:正常的建议,但都没用。


当前回答

这对我来说很管用:

<style type="text/css">
    td {

        /* CSS 3 */
        white-space: -o-pre-wrap;
        word-wrap: break-word;
        white-space: pre-wrap;
        white-space: -moz-pre-wrap;
        white-space: -pre-wrap;
    }

表属性为:

   table {
      table-layout: fixed;
      width: 100%
   }

</style>

其他回答

默认情况下,表格自动换行,所以确保显示的表格单元格为table-cell:

td {
   display: table-cell;
}

虽然可能性不大,但请使用Firebug(或类似工具)仔细检查,确保您没有意外继承以下规则:

white-space:nowrap;

这可能会覆盖您指定的换行行为。

<td style="word-break:break-all;">longtextwithoutspace</td>

or

<span style="word-break:break-all;">longtextwithoutspace</span>

下面是我在ie浏览器中使用的。注意添加了table-layout:fixed CSS属性

td { 边框:1px实体; } <table style=" font - family:宋体;宽度:100%”> < tr > < span style=" font - family:宋体 LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongWord 道明> < / < / tr > 表> < /

<p style="overflow:hidden; width:200px; word-wrap:break-word;">longtexthere<p>