【发布时间】:2016-11-23 09:15:52
【问题描述】:
我正在尝试设计一个具有粘性thead 和粘性行标题的表格。所以,基本上,所有th 元素都必须是粘性的。
我偶然发现了position: stickycss3 属性,它似乎是这项工作的理想选择,尽管许多浏览器尚不支持它(这对我来说不是问题)。但是 MDN 文档说:
'position:sticky' 对表格元素的影响与'position: relative' 相同。
考虑到这一点,我构建了一个在 Safari 10.0 中工作的基本示例,即使粘性元素的边界没有保留。
在 Firefox 50.0 中,不显示边框但不显示标题。
所以,我的问题是:如何使用position: sticky 干净地实现这个固定的标题定位。似乎实施(实施时)不完整,表的支持更少。
如果不可能,我也愿意使用 JavaScript 解决方案来实现这一点(但是将 jQuery 添加到我的堆栈中会非常麻烦,因为我的整个应用程序都在响应)。
这是我现在拥有的代码 sn-p。请注意,为了有一些粘性标题,您基本上需要 safari 或 chrome 的 alpha 版本。
div#container {
height: 200px;
width: 300px;
overflow: auto;
}
table {
border-collapse: collapse;
}
tbody th {
position: -webkit-sticky;
position: sticky;
left: 0px;
}
thead {
position: -webkit-sticky;
position: sticky;
top: 0px;
}
th {
background: #B8C1C8;
border: 2px solid black;
}
<div id="container">
<table>
<thead>
<tr>
<th>hehe</th>
<th>hello</th>
<th>world</th>
<th>hello</th>
<th>world</th>
<th>hello</th>
<th>world</th>
<th>hello</th>
<th>world</th>
<th>hello</th>
<th>world</th>
</tr>
</thead>
<tbody>
<tr>
<th>I'm a super long header</th>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<th>I'm a super long header</th>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<th>I'm a super long header</th>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<th>I'm a super long header</th>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
<td>hello</td>
<td>world</td>
</tr>
</tbody>
</table>
</div>
我尝试过的资源:
- a great article from typanus
-
Position sticky on thead 这让我在
position: sticky的一些干净用法方面有了很好的指导 - the
position: stickyspec
【问题讨论】:
标签: css html-table css-position sticky fixed-header-tables