可以使用CSS中的overflow
属性来实现响应式表格滚动效果。具体实现步骤如下:
overflow: auto;
属性,使得表格内容超出容器高度时,自动出现滚动条。.table-container {
height: 300px;
overflow: auto;
}
<thead>
和<tbody>
元素设置display: block;
属性,使其变成块级元素,可以设置宽度和高度。thead, tbody {
display: block;
}
<tr>
设置display: table;
属性,使其变成表格行。tr {
display: table;
width: 100%;
table-layout: fixed;
/* 设置表格宽度自适应 */
}
<td>
设置display: table-cell;
属性,使其变成表格单元格。td {
display: table-cell;
padding: 6px;
text-align: left;
vertical-align: top;
border-top: 1px solid #ddd;
}
完整的CSS代码如下:
.table-container {
height: 300px;
overflow: auto;
}
thead, tbody {
display: block;
}
tr {
display: table;
width: 100%;
table-layout: fixed;
}
td {
display: table-cell;
padding: 6px;
text-align: left;
vertical-align: top;
border-top: 1px solid #ddd;
}
关键词高亮:overflow
、display: block;
、display: table;
、display: table-cell;
、table-layout: fixed;
。