How to make a table cell to stick on top and most left side of table at same time?

一世执手 提交于 2020-04-30 09:37:27

问题


I have created table with sticky header using position property position:sticky; top:0;initially and then overwriting it for the first in first column using position:sticky; **left:0**;. But it is not working properly and I don't know where is the actual problem is but heading of column 1 seems sticky when previously set background color of is removed and text flows behind it otherwise it gets overlapped by upcoming headings. Kindly help me in resolving this.

<!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; z-index: 1; /*use z-index to make heading sticky on scroll with sticky column*/ }
    /* 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;
      font-size: 14px;
    }
    th {
     /*
     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;*/
      background: linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;
    }
  th:nth-child(1) {
      color: #66FCF1;
      position: sticky !important;
      left: 0 !important;
      top:0 !important;
      border: 1px solid red;
     /* background: linear-gradient(#041230,#041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat,
    red;*/
    }
  </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>#</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>

         


回答1:


Keep z-index only for that cell:

.tableFixHead thead th:nth-child(1) {
  z-index: 1;
}

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

.tableFixHead thead th {
  position: sticky;
  top: 0;
}


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

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

table {
  border-spacing: 0;
   font-size: 14px;
}

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;
  position: sticky;
  left: 0;
  background: 
    linear-gradient(#041230, #041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat, 
    red;
}

.tableFixHead thead th:nth-child(1) {
  color: #66FCF1;
  left: 0;
  background: 
    linear-gradient(#041230, #041230) center/calc(100% - 2px) calc(100% - 2px) no-repeat, 
    red;
  z-index: 1; /* added */
}
<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">

<div class="container">

  <div class="tableFixHead">
    <table class="tablecolor2">
      <thead>
        <tr>
          <th>#</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 -->


来源:https://stackoverflow.com/questions/61351332/how-to-make-a-table-cell-to-stick-on-top-and-most-left-side-of-table-at-same-tim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!