[英]Fixed header with in a scrollable div
I have a bootstrap table with in a scrollable div.我有一个带有可滚动 div 的引导表。 Wrapping table with in div help me fix my th and td alignment issues but with this my table headers also scroll and I want them to be fixed.
用 div 包装表格帮助我解决了我的 th 和 td 对齐问题,但是我的表格标题也会滚动,我希望它们得到修复。
<div class="table-wrapper">
<table class="table" id="tblfiles">
<thead>
<tr>
<th data-field="Date" class="col-md-2" style="width:100vw;">Alert Date</th>
<th data-field="Files" class="col-md-2" style="width:100vw;">Files</th>
<th data-field="fileName" class="col-md-2" style="width:100vw;">Received Time</th>
<th data-field="ReceivedTime" class="col-md-2" style="width:100vw;">Received Time</th>
<th data-field="Delete" class="col-md-1" style="width:100vw;"></th>
</tr>
</thead>
<tbody id="tblBodyFiles">
</tbody>
</table>
</div>
CSS CSS
.table-wrapper {
max-height: 200px;
overflow: auto;
display: inline-block;
} }
All I want to do now is make th fixed, Thanks a lot.我现在要做的就是修复,非常感谢。 Help is much appreciated
非常感谢帮助
You can absolute position the thead row and give the table some padding so the first row doesn't move under it.您可以绝对定位 thead 行并为表格提供一些填充,以便第一行不会在其下方移动。 This way the header row stays fixed to the top of the table.
这样标题行就固定在表格的顶部。
table {
position: relative;
padding-top: 20px;
}
thead {
position: absolute;
top: 0;
left: 0;
}
Add position:sticky
to each th
element.添加
position:sticky
到每个th
元素。 You have to do it to the th
elements, not the thead
or tr
elements.您必须对
th
元素执行此操作,而不是对thead
或tr
元素执行此操作。 This should get you started:这应该让你开始:
.table-wrapper { height: 120px; overflow: auto; border: 1px solid blue; } th { position: sticky; padding: 0px; top: 0px; background: white; }
<div class="table-wrapper"> <table> <thead> <tr> <th>header</th> </tr> </thead> <tbody> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> <tr> <td>cell</td> </tr> </tbody> </table> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.