我一直在使用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中断。我尝试了以下添加文本换行:抑制和文本换行:正常的建议,但都没用。


当前回答

在我的例子中,我必须同时使用换行和空白预换行来达到最佳效果。希望它能帮助到更多人。

<td class="wrap-longtext">
   long,longlonglonglonglonglonglong..................
</td>

<style>    
  .wrap-longtext
  {
    word-break: break-all;/* Use break-word, if you prefer sensible text than short width of the cell */
    white-space: pre-wrap;
  }
</style>

其他回答

这可能会有帮助,

css

.wrap-me{ word-break: break-all !important; } <table> <thead> <tr> <td>Col 1</td> <td>Col 2</td> <td>Col 3</td> <td>Col 4</td> <td>Col 5</td> <td>Col 6</td> <td>Col 7</td> </tr> </thead> <tbody> <tr> <td class="wrap-me">tesetonlywithoutspacetesetonlywithoutspacetesetonlywithoutspace</td> <td class="wrap-me">test only</td> <td class="wrap-me">test with space long text</td> <td class="wrap-me">Col 4</td> <td class="wrap-me">Col 5</td> <td class="wrap-me">Col 6</td> <td class="wrap-me">Col 7</td> </tr> <tr> <td class="wrap-me">test only</td> <td class="wrap-me">test only</td> <td class="wrap-me">test with space long text</td> <td class="wrap-me">testwithoutspacetestonlylongtext</td> <td class="wrap-me">Col 5</td> <td class="wrap-me">Col 6</td> <td class="wrap-me">Col 7</td> </tr> </tbody> </table>

在我的例子中,我必须同时使用换行和空白预换行来达到最佳效果。希望它能帮助到更多人。

<td class="wrap-longtext">
   long,longlonglonglonglonglonglong..................
</td>

<style>    
  .wrap-longtext
  {
    word-break: break-all;/* Use break-word, if you prefer sensible text than short width of the cell */
    white-space: pre-wrap;
  }
</style>

更改代码

word-wrap: break-word;

to

word-break:break-all;

例子

<table style="width: 100%;"> < tr > < td > < div风格= "单词分割:打破所有;长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容,长内容</div> 道明> < / <span style=" font - family:宋体;"< / >短内容跨度> 道明> < / < / tr > 表> < /

看起来你需要设置word-wrap:break-word;在块元素(div)上,具有指定的(非相对的)宽度。例:

<表格样式=“宽度:100%;”><tr> <td><div style=“display:block; word-wrap: break-word; width:40em;”>looooo</div></td> <td><span style=“display:inline;”>Foo</span></td> </tr></table>

或者根据Abhishek Simon的建议使用word-break:break-all。

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

td {
   display: table-cell;
}