如何固定html表格头部,用css样式即可实现,操作简便、代码简单

简介: 如何固定html表格头部,用css样式即可实现,操作简便、代码简单

首先创建一个表格

表头:

<div class="table_box">
  <table cellspacing="0" cellpadding="0">
    <thead>
      <tr>
        <th><input type="checkbox" class="thItem" onclick="fn(this)"></th>
        <th>姓名</th>
        <th>岗位工资</th>
        <th>岗位级别</th>
        <th>性别</th>
        <th>生日</th>
        <th>手机号</th>
        <th>身份证号</th>
        <th>项目名称</th>
        <th>公司名称</th>
        <th>政治面貌</th>
        <th>籍贯</th>
        <th>学历</th>
        <th>创建时间</th>
        <th>员工状态</th>
        <th>用工形式</th>
        <th>现合同到期时间</th>
        <th>现合同期限</th>
        <th>银行卡卡号</th>
        <th>工资卡开户行</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody id="tb">
    </tbody> 
    </table>
</div>

tbody部分直接使用js,意思是使用循环在表格的主体部分加入了表格标签,不用大量的去复制空格子(其实就是up比较懒 (o▽`o) )

<script>
      let inp = document.getElementById("tb")
      let str = ""
      for(let i = 0 ; i < 20 ; i++){
        str += `<tr>
          <td><input type="checkbox" class="thItem""></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>`
      }
      inp.innerHTML = str
    </script>

添加基本的css样式:

<style>
      .table_box{
        width: 1632px;
        overflow: scroll;
      }
      table{
        width: 3000px;
      }
      td,th{
        border: 1px solid #777;
        width: 200px;
      }
      td{
        height: 30px;
        text-align: center;
      }
    </style>

效果:

下面添加css样式,给需要固定的地方加粘性定位sticky

使用伪类选择器进行选定元素:

/* 第一列 */
      thead>tr>th:first-child{
        position: sticky;
        left: 0;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:first-child{
        position: sticky;
        left: 0;
        background-color: rgb(240, 240, 240);
      }
      /* 第二列 */
      thead>tr>th:nth-child(2){
        position: sticky;
        left: 144px;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:nth-child(2){
        position: sticky;
        left: 144px;
        background-color: rgb(240, 240, 240);
      }
    /* 最后一列 */
      thead>tr>th:last-child{
        position: sticky;
        right: 0;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:last-child{
        position: sticky;
        right: 0;
        background-color: rgb(240, 240, 240);
      }

效果:

完整代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <style>
      .table_box{
        width: 1632px;
        overflow: scroll;
      }
      table{
        width: 3000px;
      }
      td,th{
        border: 1px solid #777;
        width: 200px;
      }
      td{
        height: 30px;
        text-align: center;
      }
      /* 第一列 */
      thead>tr>th:first-child{
        position: sticky;
        left: 0;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:first-child{
        position: sticky;
        left: 0;
        background-color: rgb(240, 240, 240);
      }
      /* 第二列 */
      thead>tr>th:nth-child(2){
        position: sticky;
        left: 144px;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:nth-child(2){
        position: sticky;
        left: 144px;
        background-color: rgb(240, 240, 240);
      }
      /* 最后一列 */
      thead>tr>th:last-child{
        position: sticky;
        right: 0;
        background-color: rgb(240, 240, 240);
      }
      tbody>tr>td:last-child{
        position: sticky;
        right: 0;
        background-color: rgb(240, 240, 240);
      }
    </style>
  </head>
  <body>
    <div class="table_box">
      <table cellspacing="0" cellpadding="0">
        <thead>
          <tr>
            <th><input type="checkbox" class="thItem" onclick="fn(this)"></th>
            <th>姓名</th>
            <th>岗位工资</th>
            <th>岗位级别</th>
            <th>性别</th>
            <th>生日</th>
            <th>手机号</th>
            <th>身份证号</th>
            <th>项目名称</th>
            <th>公司名称</th>
            <th>政治面貌</th>
            <th>籍贯</th>
            <th>学历</th>
            <th>创建时间</th>
            <th>员工状态</th>
            <th>用工形式</th>
            <th>现合同到期时间</th>
            <th>现合同期限</th>
            <th>银行卡卡号</th>
            <th>工资卡开户行</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody id="tb">
        </tbody>
      </table>
    </div>
    <script>
      let inp = document.getElementById("tb")
      let str = ""
      for(let i = 0 ; i < 20 ; i++){
        str += `<tr>
          <td><input type="checkbox" class="thItem""></td>
          <td>张三</td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td>备注</td>
        </tr>`
      }
      inp.innerHTML = str
    </script>
  </body>
</html>

此文所实现的效果为纯css,适合新手,操作简单,如果使用js实现此效果的话会更加美观、简便,以后有机会还会一同分享有关js的实现方法 (▽`)

谢谢观看(/≧▽≦)/

目录
相关文章
|
17天前
|
前端开发 JavaScript 开发工具
【HTML/CSS】入门导学篇
【HTML/CSS】入门导学篇
23 0
|
23天前
|
缓存 前端开发
前端代码整洁与规范之CSS篇
【4月更文挑战第2天】 前端代码整洁与规范之CSS篇
41 4
|
1天前
|
JavaScript 前端开发 BI
原生html—摆脱ps、excel 在线绘制财务表格加水印(html绘制表格js加水印)
原生html—摆脱ps、excel 在线绘制财务表格加水印(html绘制表格js加水印)
6 1
|
7天前
|
数据采集 前端开发 网络协议
如何使用代理IP通过HTML和CSS采集数据
如何使用代理IP通过HTML和CSS采集数据
|
8天前
|
前端开发 JavaScript 开发者
html标签的样式
【4月更文挑战第19天】html标签的样式
11 2
|
10天前
|
前端开发
HTML代码示例
HTML代码示例
13 1
|
10天前
|
搜索推荐
当使用HTML代码时,一些常见的问题
当使用HTML代码时,一些常见的问题
10 0
|
11天前
错误或拦截页面的html代码
错误或拦截页面的html代码
14 0
错误或拦截页面的html代码
|
12天前
|
前端开发 搜索推荐 数据安全/隐私保护
HTML标签详解 HTML5+CSS3+移动web 前端开发入门笔记(四)
HTML标签详解 HTML5+CSS3+移动web 前端开发入门笔记(四)
18 1
|
12天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册

相关实验场景

更多