



| 列标题 1 |
列标题 2 |
列标题 3 |
列标题 4 |
| 数据 A1 | 数据 A2 | 数据 A3 | 数据 A4 |
| 数据 B1 | 数据 B2 | 数据 B3 | 数据 B4 |
| 数据 Z1 | 数据 Z2 | 数据 Z3 | 数据 Z4 |
.table-container {
height: 300px; /* 控制整个表格区域的高度 */
overflow-y: auto; /* 让整个容器滚动 */
position: relative; /* 为 sticky 定位提供参考 */
}
.table-container table {
width: 100%;
border-collapse: collapse; /* 消除单元格间距 */
table-layout: fixed; /* 帮助固定列宽,避免内容撑开 */
}
.table-container thead {
position: sticky; /* 关键:让表头粘性定位 */
top: 0; /* 粘在容器顶部 */
background-color: #f0f0f0; /* 背景色很重要,防止内容透过 */
z-index: 10; /* 确保表头在滚动内容之上 */
}
.table-container th,
.table-container td {
padding: 8px;
border: 1px solid #ddd;
text-align: left;
/* 这里的宽度需要根据实际列数和布局调整 */
width: 25%; /* 假设有4列,每列25% */
}
/* 如果需要表体单独滚动,则需要更复杂的结构 */
/* 另一种思路:让 tbody 独立滚动 */
/* .table-container {
width: 100%;
}
.table-container table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
.table-container thead {
display: table;
width: 100%;
table-layout: fixed;
}
.table-container tbody {
display: block;
height: 200px; /* 表体高度 */
overflow-y: auto;
width: 100%; /* 保证 tbody 宽度 */
}
.table-container th,
.table-container td {
width: 25%; /* 确保 th 和 td 宽度一致 */
padding: 8px;
border: 1px solid #ddd;
text-align: left;
}
.table-container tr {
display: table;
width: 100%;
table-layout: fixed;
}
*/