我正在寻找一种方法来创建具有固定宽度单元格的表格。当窗口太窄时,应该出现水平滚动条。
在我的示例中,有两个 400 像素的列,因此表格的宽度应为 800 像素。当屏幕宽度小于 800px 时,会出现水平滚动条。这是我正在寻找的确切行为: http://jsbin.com/xeniwovole/edit?html,css,output
现在问题来了:不指定表格宽度能做到吗?在现实生活中,表格的列数是动态的,列宽是响应式的。因此,尝试将表格宽度计算为列宽之和是不合理的。
最佳答案
使用 min-width
和 max-width
而不是 width
在表格单元格上。这将防止表格调整 <td>
的大小。以 100% 宽度适合表格,这是其默认行为。
#wrap {
overflow-x: auto;
}
table {
table-layout: fixed;
width: auto;
}
td {
background-color: yellow;
min-width: 400px;
max-width: 400px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="wrap">
<table>
<tr>
<td>First</td>
<td>Second</td>
</tr>
</table>
</div>
</body>
</html>
关于html - 具有固定宽度列的表格 - 不指定表格宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372571/