html - 如何防止表格单元格在 CSS 中换行?

标签 html css

我有一个 HTML 文档,其中包含一些这样的表格:

<table>
    <tr>
        <td>
            term
        </td>
        <td>
            this is a definition
        </td>
    </tr>
</table>

看起来像这样:

 _____________________________
| term | this is a definition |
|______|______________________|

当我在第一列中键入一个由两个词组成的词时,例如“Web 浏览器”,结果如下:

 _____________________________
| Web     | a tool for using  |
| browser | the WWW           |
|_________|___________________|

如何在 CSS 中调整此行为,以便只有当内容超过表格宽度的 50% 时,第一列才会换行?

理想情况下,左侧单元格中的术语永远不应有换行符:

 _____________________________
| Web browser | a tool for    |
|             | using the WWW |
|_____________|_______________|

如果左侧单元格中的术语确实很长,例如超过表格宽度的 50%,则可以换行:

 _____________________________
| this is a long | .......... |        
| term           | .......... |
|________________|____________|

这在 CSS 中如何实现?

最佳答案

这样做

 html,body,table{
    width:100%;
    height:100%
 }
 tr > td:first-child{
    white-space:nowrap;
    max-width:50%;
    word-break:break-all;
 }

关于html - 如何防止表格单元格在 CSS 中换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374812/

相关文章:

javascript - 连续颜色过渡

html - 如何在div中添加渐变颜色到背景

css - 在开发工具中一次性关闭所有 CSS 样式

html - 如何CSS选择所有div到从具有ID的div向下的级别

javascript - 循环生成唯一的 id javascript

javascript - 使用jquery改变div的颜色

javascript - 如何添加菜单以在 Bootstrap 中切换导航栏

css - bootstrap.min.css 覆盖了我的自定义 css 文件

css - 无法让 div 扩大以包含所有 float 元素

javascript - jQuery UI : Best way to create two drag/drop lists where one is a Sortable, 其他办法就掉了吗?