【问题标题】:How to fix Table Headers at the top using CSS?如何使用 CSS 修复顶部的表头?
【发布时间】:2020-10-09 05:40:15
【问题描述】:

我希望我的表头固定在顶部,并且我在标题中使用Position: sticky;
但它们仅在滚动到一半时保持固定,之后它们会随之移动。

这就是我所做的

<table>
    <thead>
        <th>Firstname</th>
        <th>Lastname</th>
    </thead>
    <tbody>
        <tr>
            <td class="scroll-table">John</td>
            <td class="scroll-table">Doe</td>
        </tr>
    </tbody>
</table>

我正在使用的 CSS

table thead{
    position: sticky;
    top: 0;
    z-index: 11;
}

我希望标题在滚动过程中停留在顶部

【问题讨论】:

  • post html 也用 sn-p &lt;&gt; 所以我们可以用一个例子代替。

标签: html css


【解决方案1】:

尝试将此值应用于th 元素而不是thead

table th {
    top: 0;
    position: sticky;
    z-index: 11;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2014-07-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2020-06-27
    • 2023-02-23
    相关资源
    最近更新 更多