简体   繁体   English

Position 贴在广告上

[英]Position sticky on thead

As you might know, position: sticky;您可能知道, position: sticky; has landed in Webkit ( demo ).已登陆 Webkit(演示)。 So far I can see this only works within the parent element.到目前为止,我可以看到这只适用于父元素。 But I'd like to know if I can use this in a scrolling div with a table.但我想知道我是否可以在带有表格的滚动 div 中使用它。

So it needs to 'listen' on the scrolling event of the div , not the table .所以它需要“监听” div的滚动事件,而不是table

I know I can do this with javascript and absolute positioning, but I was wondering if the sticky-positioning would support this.我知道我可以用 javascript 和绝对定位来做到这一点,但我想知道sticky-positioning是否支持这一点。

Position sticky on thead th works in 2018!位置粘在THEAD在2018作品!

In your stylesheets just add this one line:在您的样式表中添加这一行:

thead th { position: sticky; top: 0; }

Your table will need to include thead and th for this to style.您的表格需要包含theadth以进行样式设置。

<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>

Also, if you have multiple rows in thead , you can select the first one to remain sticky:此外,如果您在thead 中有多行,您可以选择第一行以保持粘性:

thead tr:first-child th { position: sticky; top: 0; }

As of March 2018 support is pretty much there across modern browsers ref: https://caniuse.com/#feat=css-sticky截至 2018 年 3 月,现代浏览器的支持几乎已经存在参考: https : //caniuse.com/#feat=css-sticky

Credit goes to @ctf0 for this one ( ref comment made 3 Dec 2017 )这个归功于@ctf0( 参考评论于 2017 年 12 月 3 日发表

If you need sticky header for chrome only then you can set position: sticky; top: some_value如果您只需要 chrome 的粘性标题,那么您可以设置position: sticky; top: some_value position: sticky; top: some_value ( top property is mandatory) for td element in a thead element. position: sticky; top: some_valuetop属性是强制的),用于td在元件thead元件。

See:看:

<table border=1>
  <thead>
    <tr>
      <td style='position: sticky; top: -1px;background: red'>Sticky Column</td>
      <td>Simple column</td>
    </tr>
  </thead>

table with a stiky header带有粘性标题的表格

position: sticky doesn't work with table elements (as long as their display attribute starts with table- ) since tables are not part of specification : position: sticky不适用于表格元素(只要它们的display属性以table-开头),因为表格不是规范的一部分:

Other kinds of layout, such as tables, "floating" boxes, ruby annotations, grid layouts, columns and basic handling of normal "flow" content, are described in other modules.其他类型的布局,例如表格、“浮动”框、ruby 注释、网格布局、列和正常“流”内容的基本处理,在其他模块中进行了描述。


Edit : As Jul 2019 according to https://caniuse.com/#feat=css-sticky Firefox supports this feature and Chrome has at least support for <th> tag.编辑:截至 2019 年 7 月,根据https://caniuse.com/#feat=css-sticky Firefox 支持此功能,Chrome 至少支持<th>标签。

As it turns out it position: sticky only works in the window and not in a scrolling div.事实证明它position: sticky仅适用于窗口,而不适用于滚动 div。

I created a test-case with a very long table with a table header:我创建了一个带有表头的很长表的测试用例

 h1 { font-size: 18px; font-weight: bold; margin: 10px 0; } div.testTable { height: 200px; overflow: auto; } table.stickyHead thead { position: -webkit-sticky; top: 0px; background: grey; } table.stickyHead td, table.stickyHead th { padding: 2px 3px; }
 <h1>Position sticky</h1> <div class="testTable"> <table class="stickyHead"> <thead> <tr> <th>column 1</th> <th>column 2</th> <th>column 3</th> <th>column 4</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </tbody> </table> </div>

As you can see, if you remove the overflow from the wrapper and make your window not so tall, the table-head is sticking to the top of the window.如您所见,如果您从包装纸上取下溢出物并使窗户不那么高,则表头会粘在窗户的顶部。 I doesn't apply to the wrapping div even if you make give the div position: relative即使您给出div position: relative我也不适用于包装div position: relative

Caution:警告:

posotion:sticiky doesnt work anymore on Google Chrome in 2019, try to use fixed instead or display:inline-block posotion:sticky 在 2019 年不再在 Google Chrome 上工作,尝试使用 fixed 或 display:inline-block

Setting the position:sticky for the thead is enough, no need to set it for th :设置position : sticky就足够了,不需要设置它:

table.StickyHeader thead {
    position: sticky;
    top: 0px;
}

Tested in:测试:

Edge 105.0.1343.42, Firefox 105.0, Chrome 105.0.5195.127, Opera 91.0.4516.16 Edge 105.0.1343.42、Firefox 105.0、Chrome 105.0.5195.127、Opera 91.0.4516.16

But, Firefox can not render borders of th when position of thead is set to sticky and th has background-color:但是,当 TAD 的position设置为粘性具有背景色时, Firefox无法渲染边界:

table.StickyHeader th {
    border: 1px solid black;
    padding: 5px;
    background-color: gold;
    text-align: center;
}

Firefox Firefox

Edge, Chrome, Opera边缘,铬,歌剧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM