我是靠谱客的博主 着急时光,这篇文章主要介绍html怎么实现表头不动,现在分享给大家,希望可以做个参考。

本文操作环境: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,滚动条就没有了。

下面是效果图,具体完整实例代码也在下面:

ea9c595ef649689e4faf18aaa23d281.png

完整实例代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>纯CSS table表格 thead固定 tbody滚动</title> <style> .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; } </style> </head> <body> <section> <table cellpadding="0" cellspacing="0"> <caption>纯CSS table表格 thead固定 tbody滚动</caption> <thead> <tr> <th>序 号</th> <th>姓 名</th> <th>年 龄</th> <th>性 别</th> <th>手 机</th> </tr> </thead> <tbody> <tr> <td>001</td> <td>Name</td> <td>28</td> <td>女</td> <td>Mobile</td> </tr> <tr> <td>002</td> <td>Name</td> <td>28</td> <td>男</td> <td>Mobile</td> </tr> <tr> <td>003</td> <td>Name</td> <td>28</td> <td>女</td> <td>Mobile</td> </tr> <tr> <td>004</td> <td>Name</td> <td>28</td> <td>男</td> <td>Mobile</td> </tr> <tr> <td>005</td> <td>Name</td> <td>28</td> <td>女</td> <td>Mobile</td> </tr> <tr> <td>006</td> <td>Name</td> <td>28</td> <td>男</td> <td>Mobile</td> </tr> <tr> <td>007</td> <td>Name</td> <td>28</td> <td>女</td> <td>Mobile</td> </tr> <tr> <td>008</td> <td>Name</td> <td>28</td> <td>男</td> <td>Mobile</td> </tr> </tbody> <tfoot> <tr> <td colspan="5">【table,thead,tbody,tfoot】 colspan:合并行, rowspan:合并列 </td> </tr> </tfoot> </table> </section> </body> </html>
登录后复制

【推荐学习:html视频教程】

以上就是html怎么实现表头不动的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是着急时光最近收集整理的关于html怎么实现表头不动的全部内容,更多相关html怎么实现表头不动内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部