从浮动中删除边距和填充 table header

Remove margin and padding from floating table header

滚动浏览后我贴上了 thead。我的问题是,当我将位置从 static 更改为 absolute 时,它会偏移。在更改时,它将 marginpadding 添加到 thead 中,如下所示:

我尝试使用 CSS 来修复它:

<thead id='tableHeader' style="margin:0;border:0;padding:0;">
</thead>

我想删除边距和填充以保持 table header (1871 * 37)

的原始尺寸

position:absolute 添加到 thead 后,它会失去 table 的上下文,因为它已从文档流中取出。由于它的上下文不再具有 table,因此它将自行调整其认为合适的大小。尝试添加

width: 1759px; /* Or whatever your table width is */ 

作为应用位置时的附加参数:绝对;

要解决动态大小表的大小问题,您可以像这样使用 jQuery:

 $('#tableHeader').width($('table#tableID').width()), 

或纯 JS,如果你不使用 jQuery:

 document.querySelector('#tableHeader').style.width = document.querySelector('table#tableID').style.width;