原生table:表格table中thead固定,tbody超出高度出现滚动条
# 1.表格的table-layout属性
tableLayout有如下三个值:auto fixed inherit
# 1.1 auto:
默认。列(td,th)宽度由单元格内容设定。 (1) 给td指定的wsidth不一定生效,td的width会自动调整
(2) text-overflow: ellipsis也会失效,同样,img会撑大td
# 1.2 fixed:
列宽由表格宽度和列宽度设定。
(1) 给td指定的width会生效
(2) text-overflow: ellipsis会生效
# 1.3 inherit
规定应该从父元素继承 table-layout 属性的值。
# 2.注意点
2.1 td的text-overflow : ellipsis和overflow : hidden都会起作用
2.2 text-overflow : ellipsis生效的前提是overflow不为visible,最好是类似hidden的值需要指定table的width
2.3 如果td的宽度加起来超过table的width,即使给table加上overflow:hidden,td也不会被隐藏。
# 3.代码
table tbody {
display: block;
height: 195px;
overflow-y: scroll;
}
table thead,
tbody tr {
display: table;
width: 100%;
table-layout: fixed;/*重要 表格固定算法*/
}
table thead {/*留出滚动条的位置*/
width: calc(100% - 1em)
}
table thead th {
background: #ccc;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2024/12/31, 20:25:20