table 的粘性 <td> 正在覆盖粘性 header <th>。 <td> 边框在滚动时消失。如何解决这个问题?

Sticky <td> of table is covering the sticky header <th>. and <td> border get disappered on scroll. How to resolve this?

我想创建一个 table,在 scroll-up 上修复 header,在 scroll-left 上修复第一列。我分别使用 'position: sticky' 和 'top:0' & 'left:0' 属性 和每个 child() 。但是在向上滚动时它会覆盖 header 中的标题,并且在向左滚动时粘性边框会被删除。请帮我解决这个问题。

<!DOCTYPE html>
<html>
<head>
  <style>
    /* CUSTOM */
    .tableFixHead {
      overflow-y: auto;
      max-height: 300px;
      margin-left: -15px;
      margin-right: -15px;
      margin-bottom: 100px;
      /*min-height: 200px;
      max-height: 500px;*/
      /* min-width: 600px;*/
    }
    .tableFixHead thead th { position: sticky; top: 0; }
    /* Borders */
    .tableFixHead,
    .tableFixHead td {
      box-shadow: inset 1px -1px #293033;
    }
    .tableFixHead th {
      box-shadow: inset 1px 1px #293033, 0 1px #293033;
    }

    table {
      border-spacing: 0;
    }

    th {
     /* t
     ext-align: center;*/
      padding: 8px;
      /*padding-left: 20px;*/
      color: #e9ecef;
      background-color: #1e2324;
      font-weight: 500;
      font-size: 14px;
    }
    thead{
      text-align: center;
    }
    td {
      background-color: #041230;
      text-align: center;
      padding: 10px;
      color: #e9ecef;
      min-width: 150px;
      font-size: 13px;
      word-spacing: 2px;
    }

    td:nth-child(1) {
      min-width: 50px;
      max-width: 50px;
      font-weight: bold;
      color: #66FCF1;
      position: sticky;
      left: 0;
      border: 1px solid red;
    }
    .fix-table
    {
     /* margin-top: 100px;*/
    }
  </style>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="resCSS.css">



</head>

<body>

  <div class="container">

    <div class="tableFixHead">
      <table class="tablecolor2">
        <thead>
          <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
            <th>Header 4</th>
            <th>Header 5</th>
            <th>Header 6</th>
            <th>Header 7</th>
            <th>Header 8</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
        </tbody>
      </table>
    </div>
      <!-- Table Dormant / Discontinued -->









    </div><!--  container -->

</body>
</html>

         

第 th 个元素使用 z-index 并考虑用背景替换左侧元素的边框

检查下面代码中的注释:

.tableFixHead {
  overflow-y: auto;
  max-height: 300px;
  margin-left: -15px;
  margin-right: -15px;
  margin-bottom: 100px;
}

.tableFixHead thead th {
  position: sticky;
  top: 0;
  z-index:1; /* added */
}

/* Borders */
.tableFixHead,
.tableFixHead td {
  box-shadow: inset 1px -1px #293033;
}

.tableFixHead th {
  box-shadow: inset 1px 1px #293033, 0 1px #293033;
}

table {
  border-spacing: 0;
}

th {
  padding: 8px;
  color: #e9ecef;
  background-color: #1e2324;
  font-weight: 500;
  font-size: 14px;
}

thead {
  text-align: center;
}

td {
  background-color: #041230;
  text-align: center;
  padding: 10px;
  color: #e9ecef;
  min-width: 150px;
  font-size: 13px;
  word-spacing: 2px;
}

td:nth-child(1) {
  min-width: 50px;
  max-width: 50px;
  font-weight: bold;
  color: #66FCF1;
  /* added */
  background:
    linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;
  /**/
  position: sticky;
  left: 0;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

  <div class="container">

    <div class="tableFixHead">
      <table class="tablecolor2">
        <thead>
          <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
            <th>Header 4</th>
            <th>Header 5</th>
            <th>Header 6</th>
            <th>Header 7</th>
            <th>Header 8</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
          <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
            <td>row 1, cell 3</td>
            <td>row 1, cell 4</td>
            <td>row 1, cell 5</td>
            <td>row 1, cell 6</td>
            <td>row 1, cell 7</td>
            <td>row 1, cell 8</td>
          </tr>
          <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
            <td>row 2, cell 3</td>
            <td>row 2, cell 4</td>
            <td>row 2, cell 5</td>
            <td>row 2, cell 6</td>
            <td>row 2, cell 7</td>
            <td>row 2, cell 8</td>
          </tr>
          <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
            <td>row 3, cell 3</td>
            <td>row 3, cell 4</td>
            <td>row 3, cell 5</td>
            <td>row 3, cell 6</td>
            <td>row 3, cell 7</td>
            <td>row 3, cell 8</td>
          </tr>
          <tr>
            <td>row 4, cell 1</td>
            <td>row 4, cell 2</td>
            <td>row 4, cell 3</td>
            <td>row 4, cell 4</td>
            <td>row 4, cell 5</td>
            <td>row 4, cell 6</td>
            <td>row 4, cell 7</td>
            <td>row 4, cell 8</td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- Table Dormant / Discontinued -->


  </div>
  <!--  container -->

</body>