当前位置:首页 > 行业动态 > 正文

html如何固定表头

要固定HTML表格的表头,可以使用CSS样式来实现,以下是一种常见的方法:

html如何固定表头

<style>
  table {
    bordercollapse: collapse;
    width: 100%;
  }
  th, td {
    border: 1px solid black;
    padding: 8px;
    textalign: left;
  }
  thead {
    position: sticky;
    top: 0;
    backgroundcolor: #f2f2f2;
  }
</style>
<table>
  <thead>
    <tr>
      <th>小标题1</th>
      <th>小标题2</th>
      <th>小标题3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>数据1</td>
      <td>数据2</td>
      <td>数据3</td>
    </tr>
    <!其他行 >
  </tbody>
</table>

在上面的代码中,我们使用了position: stickytop: 0来将表头固定在页面顶部。backgroundcolor属性用于设置表头的背景颜色,你可以根据需要进行修改。

html如何固定表头

请注意,这种方法在某些浏览器上可能不起作用,因为position: sticky是相对新的CSS特性,确保你的目标浏览器支持该特性。

html如何固定表头