当绝对 <div> 嵌套在其中时,相对 <th> 会失去边框

Relative <th> loses border when Absolute <div> is nested within

我有一个 TH parent DIV child。

TH 必须是 position:relative 以便其 child DIV 可以与 position:absolute 对齐。

但是,当我将 position:relative 添加到 TH 时,它完全失去了边框属性。

无论 TH 文本内容、DIV、A 或 SVG 是否存在(请参阅标记),都会发生这种情况。

我想知道如何阻止这种奇怪的行为!谢谢你。

Codepen

$(function() {
  $(".table-body").scroll(function() {
    $(".table-header")[0].style.top = (this.scrollTop) + 'px';
  });
});
body {
  font-family: 'Open Sans', sans-serif;
}

* {
  box-sizing: border-box;
}

table {
  border-collapse: collapse;
}

th,
tr,
td,
thead,
tbody {
  text-align: left;
  margin: 0 !important;
}

th,
td {
  padding: 16px 24px 16px 24px;
}

th a {
  position: relative;
  width: 24px;
  cursor: pointer;
  float: right;
  text-align: right;
}

svg * {
  transition: fill .2s ease;
}

.filters-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: red;
  height: 100px;
  width: 40px;
}

th a:hover>svg * {
  fill: #333333;
}

thead tr {
  height: 36px;
}

#bodytable tbody {
  display: block;
}

.table-header {
  position: relative;
  display: block;
}

.table-body {
  border: 1px solid #ccc;
  overflow: auto;
  height: 400px;
}

.header-cell {
  position: relative;
  background-color: #f8f8f8;
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  min-width: 330px;
}

.body-cell {
  min-width: 330px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-body">
  <table id="bodytable">
    <thead class="table-header">
      <tr>
        <th class="header-cell col1">One
          <a>
            <svg x="0px" y="0px" width="4px" height="20px" viewBox="0 0 4 20" enable-background="new 0 0 4 20" xml:space="preserve">
        <circle fill="#666666" cx="2" cy="2" r="2"/>
        <circle fill="#666666" cx="2" cy="10" r="2"/>
        <circle fill="#666666" cx="2" cy="18" r="2"/>
       </svg>
          </a>
          <div class="filters-menu"></div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="body-cell col1">body row1</td>
      </tr>
    </tbody>
  </table>
</div>

此脚本导致 table 的 header 元素垂直固定,但与 body 元素一起水平滚动。

好的,所以我发现这是 Firefox 中的一个错误,当相对定位时,TH 的背景会覆盖边界。应该搜索一下,对不起 mods。

通过在 class.header-cell

上使用透明 rgba() 背景 属性 确认

有关更多信息,请参阅此堆栈问题:

Borders not shown in Firefox with border-collapse on table, position: relative on tbody, or background-color on cell

以及用户 Boris Zbarsky 提交的错误:

https://bugzilla.mozilla.org/show_bug.cgi?id=688556

遗憾的是它在 6 年后仍然坏掉了!

目前,将背景色 属性 赋予 THEAD 父元素似乎是一个很好的解决方法

对于 firefox,您可以使用 gradientbackground-size 作为解决方法:

  background-color:#f8f8f8;/* older browser */
  background:linear-gradient(#f8f8f8,#f8f8f8 )no-repeat center / 100% 100% ;/* firefox understands this */

$(function() {
  $(".table-body").scroll(function() {
    $(".table-header")[0].style.top = (this.scrollTop) + 'px';
  });
});
body {
  font-family: 'Open Sans', sans-serif;
}

* {
  box-sizing: border-box;
}

table {
  border-collapse: collapse;
}

th,
tr,
td,
thead,
tbody {
  text-align: left;
  margin: 0 !important;
}

th,
td {
  padding: 16px 24px 16px 24px;
}

th a {
  position: relative;
  width: 24px;
  cursor: pointer;
  float: right;
  text-align: right;
}

svg * {
  transition: fill .2s ease;
}

.filters-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: red;
  height: 100px;
  width: 40px;
}

th a:hover>svg * {
  fill: #333333;
}

thead tr {
  height: 36px;
}

#bodytable tbody {
  display: block;
}

.table-header {
  position: relative;
  display: block;
}

.table-body {
  border: 1px solid #ccc;
  overflow: auto;
  height: 400px;
}

.header-cell {
  background-color:#f8f8f8;/* older browser */
  background:linear-gradient(#f8f8f8,#f8f8f8 )no-repeat center / 100% 100% ;/* firefox understands this */
  position: relative;
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  min-width: 330px;
}

.body-cell {
  min-width: 330px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-body">
  <table id="bodytable">
    <thead class="table-header">
      <tr>
        <th class="header-cell col1">One
          <a>
            <svg x="0px" y="0px" width="4px" height="20px" viewBox="0 0 4 20" enable-background="new 0 0 4 20" xml:space="preserve">
        <circle fill="#666666" cx="2" cy="2" r="2"/>
        <circle fill="#666666" cx="2" cy="10" r="2"/>
        <circle fill="#666666" cx="2" cy="18" r="2"/>
       </svg>
          </a>
          <div class="filters-menu"></div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="body-cell col1">body row1</td>
      </tr>
    </tbody>
  </table>
</div>