【发布时间】:2017-05-23 04:02:20
【问题描述】:
我正在寻找一种简单的解决方案,让表格能够水平收缩,同时保持所有列都在视图中。随着列的缩小,其中的内容应该可以水平滚动。
示例问题:
http://codepen.io/kirkouimet/pen/jybbxK
假设和目标:
- 我们不知道会有多少列
- 列将始终保持在一行(CSS 空白:nowrap)
- 理想的解决方案不涉及 JavaScript,只涉及 CSS
- 如果表格内的文本被省略了 (...),然后当您将鼠标悬停在它上面时,它会展开并且您可以水平滚动它会很棒
Codepen 链接中的代码:
HTML
<table>
<thead>
<th>First First First</th>
<th>Second Second Second</th>
<th>Third Third Third</th>
<th>Fourth Fourth Fourth</th>
</thead>
<tr>
<td>First First First</td>
<td>Second Second Second</td>
<td>Third Third Third</td>
<td>Fourth Fourth Fourth</td>
</tr>
<tr>
<td>First First First</td>
<td>Second Second Second</td>
<td>Third Third Third</td>
<td>Fourth Fourth Fourth</td>
</tr>
<tr>
<td>First First First</td>
<td>Second Second Second</td>
<td>Third Third Third</td>
<td>Fourth Fourth Fourth</td>
</tr>
</table>
CSS
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ccc;
white-space: nowrap;
}
【问题讨论】:
标签: html css html-table