CSS粘性,桌侧栏第一列不粘
对于一个项目,我需要桌子头和侧边栏(前6列)是固定/粘性的。
到目前为止,我的解决方案可以使用,但对浏览器(只有Chrome和Edge)都没有。 在Firefox和Safari中,我有一个问题,即第一列在滑到侧面和向下时不会保持固定。第2列和以下内容保持固定。
.sidebar{
position: sticky;
}
.column1{
left:0;
}
.column2{
left: 10vw;
}
.column3{
left: 20vw;
}
.column4{
left: 30vw;
}
.column5{
left: 40vw;
}
.column6{
left: 50vw;
}
<table>
<thead>
<tr>
<td class="th1">1</td>
<td class="th2">2</td>
<td class="th3">3</td>
<td class="th4">4</td>
<td class="th5">5</td>
<td class="th6">6</td>
<td> a lot more tds </td>
</tr>
</thead>
<tbody>
<tr>
<td class="sidebar column1">col 1</td>
<td class="sidebar column2">col 2</td>
<td class="sidebar column3">col 3</td>
<td class="sidebar column4">col 4</td>
<td class="sidebar column5">col 5</td>
<td class="sidebar column6">col 6</td>
<td> a lot more tds </td>
</tr>
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
位置:粘性;
在大多数现代浏览器上工作正常,您可以在此处看到https://caniuse.com/?search=position%20%3asticky%20
但是,粘性元素的行为就像相对的作用,
直到给定位置
例如,
top:0;
这是粘性的行为与元素边界达到指定位置(顶部:0)时相对的行为不同
,因此它开始像固定元素一样起作用,
所以我认为您的问题是省略了TOP或左值但是您的代码在这里不完整,您正在设置
位置:粘性;
to.sidebar
,在您的html中未使用...-= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
您的表格和TH元素也应具有
width
与您的TD元素一起,我尝试了它使用
width
,如您所述,它可以使用“ https://jsfiddle.net/mahdiar_mansouri/27k9cv6f/3/”
position:sticky;
works fine on most modern browsers as you can see herehttps://caniuse.com/?search=Position%20%3Asticky%20
BUT, sticky elements act JUST LIKE relative ones
UNTIll a given position
for example
top:0;
This is when sticky behaves different than relative
When element's boundary reaches the specified position (top:0) it start to act LIKE a fixed element
So i Think your problem is omitting top or left values however your code is not complete here, you are setting
position:sticky ;
to.sidebar
which is not used in your HTML...-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Your table and th elements should also have
width
alongside your td elements,I tried it using
width
and it worked as you describedhere's the fiddle link to it
https://jsfiddle.net/mahdiar_mansouri/27k9cv6f/3/