html - 为带有固定标题的 HTML 5 表格创建滚动条
问题描述
我想在 HTML 5 中为表格创建一个滚动条,以便固定标题。
我已经为表格创建了滚动条,但是当我向下滚动时标题会移动。我已经尝试过在 css 中使用 block: fixed 命令,但是它弄乱了整个表结构。
/* width */
::-webkit-scrollbar {
width: 10px;
}
/* Track */
::-webkit-scrollbar-track {
/*box-shadow: inset 0 0 5px grey; */
border-radius: 10px;
}
解决方案
这将解决你的问题
.table-responsive{
height:400px;
overflow:scroll;
}
thead tr:nth-child(1) th{
background: white;
position: sticky;
top: 0;
z-index: 10;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="table-responsive">
<table class="table table-hover" id="job-table">
<thead>
<tr class="text-center">
<th scope="col">Applicant ▼</th>
<th scope="col">State</th>
<th scope="col">EIN</th>
<th scope="col">DUNS</th>
<th scope="col">Grant Type</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody class="text-center tableBody">
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
<tr>
<td>City of Charlottesville Public Schools</td>
<td>Virginia</td>
<td>33-1234888</td>
<td>88-111-8235</td>
<td>Discretionary</td>
<td>Active</td>
</tr>
</tbody>
</table>
</div>
推荐阅读
- python - 如何编写将标签转换为秒的函数?
- javascript - 使用 react 渲染书架
- android - Jetpack Compose - 我的 Text() 中的版式自定义不受影响
- javascript - 将对象数组聚合成一个对象
- android - handshakeexception 客户端中的握手错误(os error wrong_version_number tls_record cc 242) Flutter 应用程序/节点服务器
- javascript - TypeError:a.preventDefault 不是函数
- git - git lab cicd 文件的内容只是做一个简单的拉
- multiprocessing - 由于 IPython 和 Windows 的限制,python 多处理现在不可用。所以将`number_workers`改为0以避免卡住
- javascript - 如何防止特定路由的中间件
- c# - 如何使用 API 在 google map xamarin iOS 上更新 PIN 中的信息