固定行标题和列标题

本文介绍了一种使用HTML和CSS实现表格滚动时保持表头固定的样式方法。通过设置特定的CSS样式,可以实现在水平或垂直滚动表格内容时,表头始终保持可见的效果。此方法适用于需要展示大量数据并保持数据条目清晰对应性的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<mce:style type="text/css"><!--
body,table, td, a{
   font-size:12px;
   text-align:center;
}
/*重点:固定行头样式*/
.scrollRowThead{
     position: relative;
     left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);
     z-index:0;
}
/*重点:固定表头样式*/
.scrollColThead{
     position: relative;
     top: expression(this.parentElement.parentElement.parentElement.scrollTop);
     z-index:2;
}
/*行列交叉的地方*/
.scrollCR{
     z-index:3;
}
/*div外框*/
.scrollDiv{
height:200px;
clear: both;
border: 1px solid #EEEEEE;
OVERFLOW: scroll;
width: 100%;
}
/*行头居中*/
.scrollColThead td,.scrollColThead th{
     text-align: center ;
}
/*行头列头背景*/
.scrollRowThead,.scrollColThead td,.scrollColThead th{
background-color:EEEEEE;
}
/*表格的线*/
.scrolltable{
border-bottom:1px solid #CCCCCC;
border-right:1px solid #CCCCCC;
}
/*单元格的线等*/
.scrolltable td,.scrollTable th{
     border-left: 1px solid #CCCCCC;
     border-top: 1px solid #CCCCCC;
     padding: 5px;
}
--></mce:style><style type="text/css" mce_bogus="1">body,table, td, a{
   font-size:12px;
   text-align:center;
}
/*重点:固定行头样式*/
.scrollRowThead{
     position: relative;
     left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);
     z-index:0;
}
/*重点:固定表头样式*/
.scrollColThead{
     position: relative;
     top: expression(this.parentElement.parentElement.parentElement.scrollTop);
     z-index:2;
}
/*行列交叉的地方*/
.scrollCR{
     z-index:3;
}
/*div外框*/
.scrollDiv{
height:200px;
clear: both;
border: 1px solid #EEEEEE;
OVERFLOW: scroll;
width: 100%;
}
/*行头居中*/
.scrollColThead td,.scrollColThead th{
     text-align: center ;
}
/*行头列头背景*/
.scrollRowThead,.scrollColThead td,.scrollColThead th{
background-color:EEEEEE;
}
/*表格的线*/
.scrolltable{
border-bottom:1px solid #CCCCCC;
border-right:1px solid #CCCCCC;
}
/*单元格的线等*/
.scrolltable td,.scrollTable th{
     border-left: 1px solid #CCCCCC;
     border-top: 1px solid #CCCCCC;
     padding: 5px;
}</style>
</head>
<body>
<div id="scrollDiv" class="scrollDiv" >
  <table border="0" cellpadding="3" cellspacing="0" width="100%" class="scrollTable">
    <tr class="scrollColThead"  >
      <th class="scrollRowThead scrollCR"  > </th>
      <th colspan="2">列头</th>
      <th colspan="10">列头</th>
    </tr>
    <tr class="scrollColThead">
      <th class="scrollRowThead scrollCR" nowrap="nowrap" width="120">序号</th>
      <th nowrap="nowrap" width="120">媒体名称</th>
      <th nowrap="nowrap" width="120">版面</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="120">行业1</th>
      <th nowrap="nowrap" width="150">行业1</th>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox" value="checkbox">a</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox2" value="checkbox">b</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox3" value="checkbox">1</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox4" value="checkbox">2</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox5" value="checkbox">3</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox6" value="checkbox">4</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td class="scrollRowThead"><input type="checkbox" name="checkbox7" value="checkbox">5</td>
      <td>单元格2</td>
      <td>单元格3</td>
      <td>单元格4</td>
      <td>单元格5</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
  </table>
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值