首页 > 解决方案 > 有没有办法将两个标题都固定在页面顶部?

问题描述

我需要将两个标题都固定在页面顶部。目前,只有第一个主题可以按我的意愿工作。

我已经尝试过position: sticky, class="position-sticky", class="position-absolute, etc" 以及 boostrap 提出的所有解决方案class,但对我来说主要问题是这两个标题属于两个不同的表,一个用于标题,一个用于主要部分。如果使用class: fixed-top它可以工作(即使只针对第一个thead),但它也覆盖了表格的一部分,这是不应该发生的事情。

这是我的 JSFiddle: https ://jsfiddle.net/m_tibo/yf8a0txL/15/

我想要实现的是能够向下滚动,同时将两个主题都固定在顶部。目前,我只能实现第一个。

谢谢您的帮助。

标签: javascriptcssbootstrap-4css-position

解决方案


你可以用这个替换你的CSS。您本质上正在寻找的是位置:固定。把它放在父母中,标题会粘住。您可能需要调整一些填充和边距才能获得所需的结果,但这应该可以解决粘性问题。

用这个 HTML 替换你的代码块:

  <section class="main">
    <table id="maintable">
      <thead class="maintable-header">
        <tr class="second-header">
          <th class="blankHeader"scope="col"></th>
          <th class="tableHeader" scope="col">Role</th>
          <th class="tableHeader" scope="col">Project</th>
          <th class="tableHeader" scope="col">Credits</th>
          <th class="tableHeader" scope="col">Year</th>
        </tr>
      </thead>

并添加这些 CSS 类:

 .second-header {
  position: fixed;
  top: 9%;
  background:#fff;
  text-align: center;
}

.tableHeader {
  width: 25%;
}

这是一个工作小提琴


推荐阅读