【发布时间】:2017-08-10 19:15:55
【问题描述】:
我想制作粘性表格标题(总是在顶部),但我有两个问题:
- thead 不会在容器 div 内溢出
- 头部不随内容滚动(在 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 的(js 只是隐藏在 html 标签 onscroll= 中)
大多数解决方案都使用两个表格而不是thead,我想避免这种情况 - 我想用thead 部分设置已经呈现的html 表格的样式。
【问题讨论】:
-
请检查这个答案。 stackoverflow.com/questions/14977864/… 如果它适合你,请投票
-
谢谢,我刚刚编辑了我的问题以解释差异
-
要控制 DOM 元素,您将需要 javascript。纯 CSS 中的任何内容都不会为您做到这一点。您可以做的最好的事情是使用内联 javascript 来避免使用任何其他工具。祝你好运只使用 CSS 找到答案:)
标签: css html-table css-tables