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. python Image 模块处理图片

    Python-Image 基本的图像处理操作,有需要的朋友可以参考下. Python 里面最常用的图像操作库是 pip install Pillow #安装模块 from PIL import Ima ...

  2. Flex 布局教程实例

    Flex 布局教程实例 一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 F ...

  3. iOS自适应行高方法及问题

    最近一周被项目的动态高度虐的很惨,感觉浪费了很多时间,但是值得高兴的是对动态高度的使用掌握了好多方法,并且知道了方法之间的区别和优缺点. 1.最常用的: UITableView+FDTemplateL ...

  4. tomcat加载java程序非常慢解决

    解决: 下面两种方式都要添加上,速度会很快,启动妙级的 1)在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy Source. 在catalina.sh中加入这么一行: JAVA_ ...

  5. Spring Cloud(6.1):搭建OAuth2 Authorization Server

    配置web.xml 添加spring-cloud-starter-security和spring-security-oauth2-autoconfigure两个依赖. </dependency& ...

  6. Unity3d NavMeshAgent 寻路问题(1)

    navMeshAgent调用setDestination 后,会有一个计算路径的时间,计算过程中pathPending为true. 在这个过程中remainingDistance一直为0.

  7. DOTS默认情况下的性能

    利用Unity全新的高性能多线程数据导向技术堆栈(DOTS),充分利用当今的多核处理器.您的游戏运行速度更快,您的代码更易于在其他项目中阅读和重用. 重建Unity的核心 我们正在使用高性能多线程数据 ...

  8. Python随笔日记(1)

    Python学习 1.安装python .之后在Windows中配置环境变量(计算机\属性\高级系统设置\环境变量\系统变量\path后加入 :路径) 2.注意变量的命名的规则 字母.数字.下划线 p ...

  9. 3rd.botan

    1.HOME 1.官网:https://botan.randombit.net/ Win下 编译步骤:https://botan.randombit.net/handbook/building.htm ...

  10. Velocity 数值格式化(NumberTool工具类):保留两位小数和格式化千分位、取整

    Velocity 自带的工具类:NumberTool 实现数字格式化:保留两位小数和格式化千分位,以及取整. NumberTool 的 format(String format, Object obj ...