html实现表头不动的方法:首先将内容要滚动的区域控制在tbody标签中,并添加“overflow-y: auto;”样式;然后给tr标签添加“table-layout:fixed;”即可固定表头。

本文操作环境:windows7系统、HTML5&&CSS3版、Dell G3电脑。
HTML table表格 固定表头 tbody加滚动条
纯CSS table表格 thead固定 tbody滚动效果
由于项目需要,在表格中,当数据量越来越多时,就会出现滚动条,而在滚动的过程中,默认情况下表格头部会跟着表格内容一起滚动,导致看不到头部对应的字段名,影响体验效果!
立即学习“前端免费学习笔记(深入)”;
实现思路:
将内容要滚动的区域控制在 tbody 标签中添加 overflow-y: auto; 样式,给 tr 标签添加 table-layout:fixed; (这是核心)样式,由于 tbody 有了滚动条后,滚动条也要占位,又会导致 tbody 和 thead 不对齐,所以在设置 tbody 的宽度时要把滚动条的宽度也加上【如果不想显示滚动条的话,可以把滚动条的宽度设置为0px,滚动条就没有了。
下面是效果图,具体完整实例代码也在下面:

完整实例代码:
纯CSS table表格 thead固定 tbody滚动 .table-box { margin: 100px auto; width: 1024px; } /* 滚动条宽度 */ ::-webkit-scrollbar { width: 8px; background-color: transparent; } /* 滚动条颜色 */ ::-webkit-scrollbar-thumb { background-color: #27314d; } table { width: 100%; border-spacing: 0px; border-collapse: collapse; } table caption{ font-weight: bold; font-size: 24px; line-height: 50px; } table th, table td { height: 50px; text-align: center; border: 1px solid gray; } table thead { color: white; background-color: #38F; } table tbody { display: block; width: calc(100% + 8px); /*这里的8px是滚动条的宽度*/ height: 300px; overflow-y: auto; -webkit-overflow-scrolling: touch; } table tfoot { background-color: #71ea71; } table thead tr, table tbody tr, table tfoot tr { box-sizing: border-box; table-layout: fixed; display: table; width: 100%; } table tbody tr:nth-of-type(odd) { background: #EEE; } table tbody tr:nth-of-type(even) { background: #FFF; } table tbody tr td{ border-bottom: none; }
纯CSS table表格 thead固定 tbody滚动 序 号 姓 名 年 龄 性 别 手 机 001 Name 28 女 Mobile 002 Name 28 男 Mobile 003 Name 28 女 Mobile 004 Name 28 男 Mobile 005 Name 28 女 Mobile 006 Name 28 男 Mobile 007 Name 28 女 Mobile 008 Name 28 男 Mobile 【table,thead,tbody,tfoot】 colspan:合并行, rowspan:合并列
【推荐学习:html视频教程】
以上就是html怎么实现表头不动的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1550146.html
微信扫一扫
支付宝扫一扫