简体   繁体   English

怎么打断长长的单词<td>使用 CSS?

[英]How to break long single word in <td> using CSS?

in a td of a table i have a very long single word " Pneumonoultramicroscopicsilicovolcanoconiosis " and i want to reduce the width of table but unable to do because of this long word in one of the td i can break this word by giving <br /> but is there any CSS way to break this word according to available space.tabletd中,我有一个很长的单词“ Pneumonoultramicroscopicsilicovolcanoconiosis ”,我想减小table的宽度,但由于其中一个td中的这个长字而无法做到,我可以通过给出<br />来打破这个词但是有没有任何CSS方法可以根据可用空间来打破这个词。 without giving line break or edit anything in HTML.无需换行或编辑 HTML 中的任何内容。

Try the following:尝试以下操作:

word-wrap: break-word;
white-space: normal;

word-wrap is css3 (however it works in IE). word-wrap 是 css3(但它在 IE 中有效)。 You might not be able to get it working in older browsers however.但是,您可能无法使其在较旧的浏览器中运行。

The following works for me:以下对我有用:

    word-wrap: break-word;
    word-break: break-all;
    white-space: normal;

Try the following code for break word if there is not any space.如果没有空格,请尝试以下代码作为断词。

word-wrap: break-word;
word-break: break-all;
td span{display:block;width:your_max_width_here.px}

<td><span>Pneumonoultramicroscopicsilicovolcanoconiosis</span></td>

try my code --试试我的代码——

With Table body带表体

table>tbody>tr>th{
    word-wrap: break-word;
    word-break: break-all;
    white-space: normal;
}

Without Table body无表体

table>tr>th{
    word-wrap: break-word;
    word-break: break-all;
    white-space: normal;
}

I've tried different combinations of solutions found here and in other pages but it never worked as expected for me - the short words were split even if they had space on the line below and it looked goofy.我已经尝试过在这里和其他页面中找到的不同解决方案组合,但它从来没有像我预期的那样工作 - 即使它们在下面的行上有空格并且看起来很傻,短词也被分割了。

Finally I've found this compromise:最后我找到了这个折衷方案:

table {
    table-layout: fixed;
    overflow-wrap: break-word;
    word-wrap: break-word; /* IE */
}

If you want to treat your rows equally, it works best.如果你想平等对待你的行,它效果最好。

以下代码应该可以工作;

td {word-break: break-all}

I found when you have a mixture of content in the cell, some long single words mixed in with other short words, that word-break: break-word;我发现当单元格中的内容混合在一起时,一些长的单个单词与其他短单词混合在一起,即word-break: break-word; works best.效果最好。 It will break on whitespace when possible.如果可能,它将在空白处中断。 Although this doesn't appear to be supported in IE11.虽然这在 IE11 中似乎不受支持。 If you set break-all to either of the mentioned properties, it will always break within a word (unless it's lucky enough to be on a space).如果您将 break-all 设置为上述任何一个属性,它总是会在一个单词内中断(除非它很幸运能够在一个空格上)。

The modern (post IE) way is:现代(后 IE)方式是:

td {
  overflow-wrap: anywhere;
}

Except that – as of this writing – it is not supported in Safari.除了 - 在撰写本文时 - 它在 Safari 中不受支持。 Stay tuned. 敬请关注。

Here is a test.这是一个测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM