css - 达到最大宽度时打破表格单元格内容
问题描述
我有一个这样的 HTML 表:
div {
width: 100%;
overflow-x: auto;
}
table {
white-space: nowrap;
}
td {
border: 1px solid gray;
padding: 5px 10px;
min-width: 100px;
max-width:40vw;
}
<div>
<table style="width: 100%;">
<tr>
<td class="block">1</td>
<td class="block">A table cell content</td>
<td class="block">This should be a text that breaks after 200px Lorem ipsum dolor sit amet</td>
<td class="block">A table cell content</td>
<td class="block">A table cell content</td>
<td class="block">A table cell content</td>
<td class="block">A table cell content</td>
</tr>
</table>
</div>
结果:
问题是,当max-width
到达时文本应该换行。我知道white-space: nowrap
会导致这个问题,但如果没有达到之前的文本换行max-width
。
解决方案
td {
border: 1px solid gray;
padding: 5px 10px;
min-width: 100px;
max-width:40vw;
white-space: pre-wrap
}
添加或删除white-space: pre-wrap
_td
white-space: nowrap
table
你可以添加span
标签td
来实现这个输出吗?
span {
white-space: pre-wrap
}
但是,你已经width:100%
在你的table
. 我已经像这样在pxwidth:1500px
中设置了它。如果您在表格中设置100% 宽度,则剩余的列将在达到其最大宽度之前换行这些文本。
你可以检查上面的小提琴,我已经设置width:100%
并table
删除了最后两列。现在,冗长的文本列将适应40vw
并且剩余的列不会预先包装文本。因此,这也取决于您的父表宽度。
推荐阅读
- php - 如何正确访问 laravel 中的范围?
- excel - 查找和替换多个值并在另一列中更新
- c++ - OpenGL EGL 在 docker 的 eglGetDisplay 返回 0x300c (EGL_BAD_PARAMETER)
- opengl-es - 我找不到做什么:函数“InitRenderingGLES”的隐式声明在 C99 中无效意味着
- sql-server - 在 Jupyter SQL 笔记本中添加本地图像
- mongodb - 每次重启服务器时Mongodb连接错误
- javascript - 尝试使用状态变量将字符串变为大写
- python-3.x - 您如何从 watson 助手内部调用 3rd 方 API?
- wordpress - 网站已被弹出式注入攻击
- crystal-reports - 水晶报表删除最后一个回车