【发布时间】:2012-01-30 22:59:50
【问题描述】:
我正在尝试为来自我们数据库的数据创建一个顶部带有固定标题的表。当我添加'位置:固定;'到标题的 css 它将它保持在顶部,但它强制整个标题到第一列。如何使表格标题位于顶部并与列正确对齐?如果可能的话,我更喜欢 css/html 解决方案。
编辑:我已经尝试了很多我在 SO 和谷歌上找到的 jQuery 解决方案。有些工作,有些不工作。当我将它们与我在页面上运行的其他脚本结合起来时,那些独立工作的人往往会崩溃......
<style>
.dg_hdr_row{
position: fixed;
top:0;
height: 25px;
}
.dg_col1{ width:60%; border: 1px solid #000; padding: 5px;}
.dg_col2{ width:15%; border: 1px solid #000; padding: 5px;}
.dg_col3{ width:10%; border: 1px solid #000; padding: 5px;}
.dg_col4{ width:15%; border: 1px solid #000; padding: 5px;}
</style>
<table width="100%">
<thead width="100%" >
<tr width="100%" class="dg_hdr_row" >
<th width="60%">Column 1</th>
<th width="15%">Column 2</th>
<th width="10%">Column 3</th>
<th width="15%">Column 4</th>
</tr>
</thead>
<tbody>
<tr class="dg_row">
<td class="dg_col1"></td>
<td class="dg_col2"></td>
<td class="dg_col3"></td>
<td class="dg_col4"></td>
</tr>
<tr class="dg_row">
<td class="dg_col1"></td>
<td class="dg_col2"></td>
<td class="dg_col3"></td>
<td class="dg_col4"></td>
</tr>
</tbody>
</table>
【问题讨论】:
-
戳完之后,它可以在 Chrome 中运行(肯定是 17,之前没有任何线索),但不能在 Firefox 或 IE 9 中运行。
-
@SKS - 我还认为这不是重复的,因为这是一种特定情况,而不是关于如何完成粘性标头的一般要求。另一个问题专门询问了一个 jQuery 插件。
-
@SKS:我尝试了很多关于该问题的“答案”,但都不起作用。
-
@derekerdmann:我正在使用 Chrome 16...我认为 17 还没有发布。它在我的任何 Chrome、IE 9 和 Firefox 中都无法正确显示。