如何仅使用 css 使粘性 table header 溢出到可滚动 div 内?
How to make sticky table header overflow inside scrollable div using only css?
我想置顶tableheader(总是在最上面),但是我有两个问题:
- thead 不会在容器内溢出 div
- thead 不随内容滚动(在 x 轴上)
这是一个例子(刚从另一个问题中分叉出来):
HTML:
<section class="">
<div class="container">
<table>
<thead>
<tr class="header">
<th>
Table attribute name
<div>Table attribute name</div>
</th>
<th>
Value
<div>Value</div>
</th>
<th>
Description
<div>Description</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>align</td>
<td>left, center, right</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
</tr>
<tr>
<td>bgcolor</td>
<td>rgb(x,x,x), #xxxxxx, colorname</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
</tr>
<tr>
<td>border</td>
<td>1,""</td>
<td>Specifies whether the table cells should have borders or not</td>
</tr>
<tr>
<td>cellpadding</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
</tr>
<tr>
<td>cellspacing</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between cells</td>
</tr>
<tr>
<td>frame</td>
<td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
<td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
</tr>
<tr>
<td>rules</td>
<td>none, groups, rows, cols, all</td>
<td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
</tr>
<tr>
<td>summary</td>
<td>text</td>
<td>Not supported in HTML5. Specifies a summary of the content of a table</td>
</tr>
<tr>
<td>width</td>
<td>pixels, %</td>
<td>Not supported in HTML5. Specifies the width of a table</td>
</tr>
</tbody>
</table>
</div>
</section>
CSS:
html, body{
margin:0;
padding:0;
height:100%;
}
section {
position: relative;
border: 1px solid #000;
padding-top: 37px;
background: #500;
width: 200px;
height: 200px;
}
table {
border-spacing: 0;
width:100%;
}
td + td {
border-left:1px solid #eee;
}
td, th {
border-bottom:1px solid #eee;
background: #ddd;
color: #000;
width: 100px;
}
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
top: 0;
line-height: normal;
border-left: 1px solid #800;
}
th:first-child div{
border: none;
}
我只想使用没有 js 技巧的 css 解决方案。列的宽度可以明确定义,但高度不能。
你能修复我的示例并解释它应该如何工作吗?
编辑:
我已经检查过问题:
Fixed header table with horizontal scrollbar and vertical scrollbar on
- 这对我来说不是解决方案,因为前两个答案使用 JQuery,@Anshuman 建议的第三个答案也是 js-based(js 只是隐藏在 html 标签 onscroll= 中)
大多数解决方案都使用两个 table 而不是 thead,我想避免这种情况 - 我想使用 thead 部分设置已经呈现的 html table 的样式。
使用您的 html 结构,您可以在容器和 tbody 上调度滚动条。但只有当容器一直向右滚动时才会看到 tbody 滚动条。
不太确定这就是您要查找的内容(用于演示的宽度和高度,如果您尝试这样做,请根据您的需要更新这些内容):
table {
display: block;
width: 75vw;
}
section {
width: 25vw;
overflow: hidden;
position: relative;
overflow-x: auto;
}
thead,
tbody {
display: block;
}
tbody {
height: 150px;
overflow-x: hidden;
overflow-y: auto;
}
tr {
display: table;
border-collapse: collapse;
width: 75vw;
table-layout: fixed;
}
th,
td,
th div {
box-shadow: 0 0 0 1px;
}
th div {
position: absolute;
top: 0;
left: auto;
width: 25vw;
background: gray;
}
<section>
<table>
<thead>
<tr class="header">
<th>
Table attribute name
<div>Table attribute name</div>
</th>
<th>
Value
<div>Value</div>
</th>
<th>
Description
<div>Description</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>align</td>
<td>left, center, right</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
</tr>
<tr>
<td>bgcolor</td>
<td>rgb(x,x,x), #xxxxxx, colorname</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
</tr>
<tr>
<td>border</td>
<td>1,""</td>
<td>Specifies whether the table cells should have borders or not</td>
</tr>
<tr>
<td>cellpadding</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
</tr>
<tr>
<td>cellspacing</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between cells</td>
</tr>
<tr>
<td>frame</td>
<td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
<td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
</tr>
<tr>
<td>rules</td>
<td>none, groups, rows, cols, all</td>
<td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
</tr>
<tr>
<td>summary</td>
<td>text</td>
<td>Not supported in HTML5. Specifies a summary of the content of a table</td>
</tr>
<tr>
<td>width</td>
<td>pixels, %</td>
<td>Not supported in HTML5. Specifies the width of a table</td>
</tr>
</tbody>
</table>
我想置顶tableheader(总是在最上面),但是我有两个问题:
- thead 不会在容器内溢出 div
- thead 不随内容滚动(在 x 轴上)
这是一个例子(刚从另一个问题中分叉出来):
HTML:
<section class="">
<div class="container">
<table>
<thead>
<tr class="header">
<th>
Table attribute name
<div>Table attribute name</div>
</th>
<th>
Value
<div>Value</div>
</th>
<th>
Description
<div>Description</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>align</td>
<td>left, center, right</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
</tr>
<tr>
<td>bgcolor</td>
<td>rgb(x,x,x), #xxxxxx, colorname</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
</tr>
<tr>
<td>border</td>
<td>1,""</td>
<td>Specifies whether the table cells should have borders or not</td>
</tr>
<tr>
<td>cellpadding</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
</tr>
<tr>
<td>cellspacing</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between cells</td>
</tr>
<tr>
<td>frame</td>
<td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
<td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
</tr>
<tr>
<td>rules</td>
<td>none, groups, rows, cols, all</td>
<td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
</tr>
<tr>
<td>summary</td>
<td>text</td>
<td>Not supported in HTML5. Specifies a summary of the content of a table</td>
</tr>
<tr>
<td>width</td>
<td>pixels, %</td>
<td>Not supported in HTML5. Specifies the width of a table</td>
</tr>
</tbody>
</table>
</div>
</section>
CSS:
html, body{
margin:0;
padding:0;
height:100%;
}
section {
position: relative;
border: 1px solid #000;
padding-top: 37px;
background: #500;
width: 200px;
height: 200px;
}
table {
border-spacing: 0;
width:100%;
}
td + td {
border-left:1px solid #eee;
}
td, th {
border-bottom:1px solid #eee;
background: #ddd;
color: #000;
width: 100px;
}
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
top: 0;
line-height: normal;
border-left: 1px solid #800;
}
th:first-child div{
border: none;
}
我只想使用没有 js 技巧的 css 解决方案。列的宽度可以明确定义,但高度不能。
你能修复我的示例并解释它应该如何工作吗?
编辑: 我已经检查过问题: Fixed header table with horizontal scrollbar and vertical scrollbar on - 这对我来说不是解决方案,因为前两个答案使用 JQuery,@Anshuman 建议的第三个答案也是 js-based(js 只是隐藏在 html 标签 onscroll= 中)
大多数解决方案都使用两个 table 而不是 thead,我想避免这种情况 - 我想使用 thead 部分设置已经呈现的 html table 的样式。
使用您的 html 结构,您可以在容器和 tbody 上调度滚动条。但只有当容器一直向右滚动时才会看到 tbody 滚动条。
不太确定这就是您要查找的内容(用于演示的宽度和高度,如果您尝试这样做,请根据您的需要更新这些内容):
table {
display: block;
width: 75vw;
}
section {
width: 25vw;
overflow: hidden;
position: relative;
overflow-x: auto;
}
thead,
tbody {
display: block;
}
tbody {
height: 150px;
overflow-x: hidden;
overflow-y: auto;
}
tr {
display: table;
border-collapse: collapse;
width: 75vw;
table-layout: fixed;
}
th,
td,
th div {
box-shadow: 0 0 0 1px;
}
th div {
position: absolute;
top: 0;
left: auto;
width: 25vw;
background: gray;
}
<section>
<table>
<thead>
<tr class="header">
<th>
Table attribute name
<div>Table attribute name</div>
</th>
<th>
Value
<div>Value</div>
</th>
<th>
Description
<div>Description</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>align</td>
<td>left, center, right</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
</tr>
<tr>
<td>bgcolor</td>
<td>rgb(x,x,x), #xxxxxx, colorname</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
</tr>
<tr>
<td>border</td>
<td>1,""</td>
<td>Specifies whether the table cells should have borders or not</td>
</tr>
<tr>
<td>cellpadding</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
</tr>
<tr>
<td>cellspacing</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between cells</td>
</tr>
<tr>
<td>frame</td>
<td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
<td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
</tr>
<tr>
<td>rules</td>
<td>none, groups, rows, cols, all</td>
<td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
</tr>
<tr>
<td>summary</td>
<td>text</td>
<td>Not supported in HTML5. Specifies a summary of the content of a table</td>
</tr>
<tr>
<td>width</td>
<td>pixels, %</td>
<td>Not supported in HTML5. Specifies the width of a table</td>
</tr>
</tbody>
</table>