Evo*_*lve 63
位置上粘THEAD日在2018年的作品!
在样式表中只需添加以下一行:
thead th { position: sticky; top: 0; }
Run Code Online (Sandbox Code Playgroud)
你的桌子需要包括thead和th为此风格.
<table>
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th>column 3</th>
<th>column 4</th>
</tr>
</thead>
<tbody>
// your body code
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
此外,如果thead中有多行,则可以选择第一行保持粘性:
thead tr:first-child th { position: sticky; top: 0; }
Run Code Online (Sandbox Code Playgroud)
截至2018年3月,现代浏览器的支持几乎都是支持ref:https://caniuse.com/#feat=css-sticky
这个奖励归功于@ ctf0(参考评论发表于2017年12月3日)
小智 13
如果您只需要chrome的标头,那么您可以为元素中的元素设置position: sticky; top: some_value(top属性是必需的).tdthead
看到:
<table border=1>
<thead>
<tr>
<td style='position: sticky; top: -1px;background: red'>Sticky Column</td>
<td>Simple column</td>
</tr>
</thead>
Run Code Online (Sandbox Code Playgroud)