如何冻结HTML表格中的标题行?

lax*_*xmi 7 html css scroll html-table

我正在使用HTML表并已应用垂直滚动,现在我想在滚动期间冻结标题行.我怎样才能做到这一点?

Dav*_*son 5

HTML:

<table>
 <tr id="header-row">
  <th>header col 1</th>
  <th>header col 2</th>
 </tr>
 <tr>
  <td>data</td>
  <td>data</td>
 <tr>
 <tr>...</tr>
 ...
</table>
Run Code Online (Sandbox Code Playgroud)

CSS:

#header-row { position:fixed; top:0; left:0; }
table {padding-top:15px; }
Run Code Online (Sandbox Code Playgroud)


gmh*_*mhk 4

一种简单的方法是创建 2 个表并固定列宽。第一个充当标题

下面的第二个表是滚动条所在的位置,并且仅显示数据。

  • 我的表行内容将动态显示,因此当我按照您建议的不同数据尝试使用两个表时,主体行会有所不同,并且标题表和主体表之间看起来不匹配 (2认同)