1、可以使用display: table; width: 100%; table-layout: fixed;

table-layout: fixed;设置表格布局算法。tableLayout 属性用来显示表格单元格、行、列的算法规则。

详细可见:https://www.w3school.com.cn/cssref/pr_tab_table-layout.asp

https://www.w3school.com.cn/tiy/t.asp?f=csse_table_table-layout

<div>
<div class="table-head">
<table id="contentTable">
<thead>
<tr>
<th><input type="checkbox"/></th>
<th>xxxxx</th>
<th>xxxxx</th>
<th>证件号码</th>
<th>姓名</th>
<th>性别</th>
<th>xxxxx</th>
<th>出生日期</th>
<th>xxx</th>
<th>xxx</th>
<th>ssss</th>
<th>xxxx</th>
<th>ccccc手机</th>
<th>家庭住址</th>
<th>xxxxx</th>
<th>xxxxx</th>
<th>xxxxx</th>
<th>修改日期</th>
</tr>
</thead>
<tbody>
<tr>
//21个td 省略
</tr>
</tbody>
</table>
</div>
</div>

这里是样式

/** 这是css */
body{overflow-y: hidden}
#contentTable tbody { display: block; height: 520px; overflow-y: scroll; }
#contentTable thead,
#contentTable tbody tr { display: table; width: 100%; table-layout: fixed; /**表格列的宽度由表格宽度决定,不由内容决定*/ text-align: center; } /**每一个th都要和对应的td宽度相同,如果不设置,则默认均分宽度,我设置的都是一些内容较多的,比如说身份证号码,电话,住址等*/
#contentTable tr th:nth-of-type(1),#contentTable tr td:nth-of-type(1){width: 1.5%;}
#contentTable tr th:nth-of-type(2),#contentTable tr td:nth-of-type(2){width: 8.5%;}
#contentTable tr th:nth-of-type(3),#contentTable tr td:nth-of-type(3){width: 7%;}
#contentTable tr th:nth-of-type(4),#contentTable tr td:nth-of-type(4){width: 8.5%;}
#contentTable tr th:nth-of-type(8),#contentTable tr td:nth-of-type(8){width: 5%;}
#contentTable tr th:nth-of-type(9),#contentTable tr td:nth-of-type(9){width: 5%;}
#contentTable tr th:nth-of-type(12),#contentTable tr td:nth-of-type(12){width: 8.5%;}
#contentTable tr th:nth-of-type(13),#contentTable tr td:nth-of-type(13){width: 6%;}
#contentTable tr th:nth-of-type(14),#contentTable tr td:nth-of-type(14){width: 15%;}
#contentTable tr th:nth-of-type(16),#contentTable tr td:nth-of-type(16){width: 5%;}
#contentTable tr th:nth-of-type(18),#contentTable tr td:nth-of-type(18){width: 5%;} /**这一个重要:写完上面的看效果应该是表头和内容对不齐。我们需要的是调节0.4em中的0.4,试着变大变小就能看到区别,我试了,刚好跳到0.4,所有的列都能垂直对齐*/
#contentTable thead { width: calc( 100% - 0.4em);/*表头与表格垂直对齐*/ }

这里如果不需要右侧滚动条的时候,

#sTable::-webkit-scrollbar{
width:0px;
opacity:0;
-webkit-overflow-scrolling: touch;
}

  

使用css让表头固定的方法的更多相关文章

  1. div+css实现表头固定内容滚动表格

    <div class="m-demo"> <table> <thead> <tr><th>定宽a</th>& ...

  2. html css 如何将表头固定(转)

    html css 如何将表头固定 position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时 ...

  3. 纯CSS实现table表头固定(自创备忘)

    因为之前约定时候产品没说要表头固定,这次迭代测试突然提出这个需求,而且不知道因为什么未知原因非要这样不可--因为之前用了table标签做表单,而且也没用插件,这下就难了点,找另外一个前端前辈妹子商量了 ...

  4. html css 如何将表头固定

    position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时必须解决两个问题.第一:表体将随之 ...

  5. css手写一个表头固定

    Bootstrap,layui等前端框架里面都对表头固定,表格滚动有实现,偏偏刚入职的公司选择了手动渲染表格,后期又觉得表格数据拉太长想要做表头固定.为了避免对代码改动太大,所以决定手写表头固定 主要 ...

  6. html bootstrap 表头固定在顶部,表列 可以自由滚动的效果

    该效果主要是依照 bootstrap 的一个样式,class="navbar-fixed-top"; 参考网址为:http://v3.bootcss.com/components/ ...

  7. IE的表头固定/表头不动(使用expression)

    本文主要介绍在IE浏览器中,实现表头固定的一种方法.这种方法使用到了 IE 浏览器特有的 expression 方法. 表头固定DEMO1 主要代码: <style type="tex ...

  8. 【转】CSS z-index 属性的使用方法和层级树的概念

    文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...

  9. css实现居中的五中方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. SQL-W3School-高级:SQL FOREIGN KEY 约束

    ylbtech-SQL-W3School-高级:SQL FOREIGN KEY 约束 1.返回顶部 1. SQL FOREIGN KEY 约束 一个表中的 FOREIGN KEY 指向另一个表中的 P ...

  2. java中的异步处理和Feature接口(一)

    背景介绍想象这样一个场景:你可能希望为你的法国客户提供指定主题的热点报道.为实现这一功能,你需要向 谷歌或者Twitter的API请求所有语言中针对该主题最热门的评论,可能还需要依据你的内部算法 对它 ...

  3. springboot之kafka安装与实践

    环境:腾讯云centos7 1.下载 http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.11-2.3.0.tgz 2.解压 tar -xvf ka ...

  4. ubuntu18 faster-rcnn

    luo@luo-All-Series:~/MyFile$ luo@luo-All-Series:~/MyFile$ luo@luo-All-Series:~/MyFile$ git clone htt ...

  5. python 时间对比

    import datetimed1 = datetime.datetime.strptime('2015-03-05  17:41:20', '%Y-%m-%d %H:%M:%S')d2 = date ...

  6. jackson将json数组转成List、普通数组。

    package com.mkyong; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jacks ...

  7. Generate Maximum revenue by selling K tickets from N windows

    Objective: Given ‘N’ windows where each window contains certain number of tickets at each window. Pr ...

  8. opengl读取灰度图生成三维地形

    准备第三方库 glew.freeglut.glm.opencv 准备灰度图片和草地贴图 最终效果 代码包括主程序源文件mainApp.cpp.顶点着色器shader.vs.片元着色器shader.fs ...

  9. jquery中的$(document).ready(function(){})和$(window).load()比较

    1.执行时间 window.onload()即jquery写法中的$(window).load(function(){})必须等到页面内包括图片的所有元素加载完毕后才能执行. $(document). ...

  10. flask上下管理文相关 - 总结

    flask上下管理文相关 - 总结 flask上下文管理机制 当用户请求到来之后,flask内部会创建两个对象: ctx = ReqeustContext(),内部封装request/sesion a ...