直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.table-box{
width: 500px;
height: 200px;
overflow-y: auto;
position: relative;
}
table{
width: 100%;
}
th,td {
padding: 20px 20px;
}
tfoot tr {
background-color: red;
}
thead{
background-color: red;
position: sticky;
top: 0;
}
tfoot{
position:sticky;
bottom: 0;
}
</style>
</head>
<body>
<div class="table-box">
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr>
<td>tt</td>
<td>tt</td>
<td>tt</td>
<td>tt</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>aa</td>
<td>aa</td>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>dd</td>
<td>dd</td>
<td>dd</td>
<td>dd</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>ccc</td>
<td>ccc</td>
<td>ccc</td>
<td>ccc</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>