-1

I have a table like below with the first row inside tbody containing the column header (I'm doing this so that column width will match the width of td cells)

I want to make the first row, row-header to float on top of the table without breaking the columnns width.

position absolute and sticky does not seem to be working.

.rdp-table {
  text-align: left;
  background-color: #fff;
  border-collapse: collapse;
  margin: 0 auto;
  overflow: scroll;
}

.rdp-table tbody {
  max-height: 600px;
  overflow-y: scroll;
}

.rdp-table tr {
  border: 1px solid #ccc;
}

.rdp-table tr:hover {
  background-color: #f5f5f5;
}

.rdp-table th,
.rdp-table td {
  font-weight: 600;
  line-height: 24px;
  border: 1px solid #cecece;
  min-width: 128px;
  text-align: center;
}

.rdp-table th {
  text-align: center;
  color: black;
  text-transform: uppercase;
}
<table id="table" class="rdp-table">
  <tbody>
    <tr class="row-header">
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
      <th>Column 6</th>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
  </tbody>
</table>
ash1f
  • 89
  • 2
  • 8
  • Does this answer your question? [Table header to stay fixed at the top when user scrolls it out of view with jQuery](https://stackoverflow.com/questions/4709390/table-header-to-stay-fixed-at-the-top-when-user-scrolls-it-out-of-view-with-jque) – Awais Jan 29 '20 at 12:36

3 Answers3

1

try this:

.rdp-table {
  text-align: left;
  background-color: #fff;
  border-collapse: collapse;
  margin: 0 auto;
  overflow: scroll;
}

.rdp-table tbody {
  max-height: 600px;
  overflow-y: scroll;
}

.rdp-table tr {
  border: 1px solid #ccc;
}

.rdp-table tr:hover {
  background-color: #f5f5f5;
}

.rdp-table th,
.rdp-table td {
  font-weight: 600;
  line-height: 24px;
  border: 1px solid #cecece;
  min-width: 128px;
  text-align: center;
}

.rdp-table th {
  text-align: center;
  color: black;
  text-transform: uppercase;
  position: sticky;
  top:0;
  background-color:#fff
}
<table id="table" class="rdp-table">
  <tbody>
    <tr class="row-header">
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
      <th>Column 6</th>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
  </tbody>
</table>
sumeshsn1
  • 756
  • 5
  • 13
0

Check the below snippet.

$(window).scroll(function() {
  var scroll = $(window).scrollTop();

  if (scroll >= 50) {
    $(".row-header").addClass("sticky");
  } else {
    $(".row-header").removeClass("sticky");
  }
});
.table-container {
  position: relative
}

.rdp-table {
  text-align: left;
  background-color: #fff;
  border-collapse: collapse;
  margin: 0 auto;
  overflow: scroll;
}

.rdp-table tbody {
  max-height: 600px;
  overflow-y: scroll;
}

.rdp-table tr {
  border: 1px solid #ccc;
}

.sticky {
  position: fixed;
  top: 0;
  left: 10px;
  right: 10px;
  z-index: 100
}

.rdp-table tr:hover {
  background-color: #f5f5f5;
}

.rdp-table th,
.rdp-table td {
  font-weight: 600;
  line-height: 24px;
  border: 1px solid #cecece;
  min-width: 128px;
  text-align: center;
}

.rdp-table th {
  text-align: center;
  color: black;
  text-transform: uppercase;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="table-container">
  <table id="table" class="rdp-table">
    <tbody>
      <tr class="row-header">
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
        <th>Column 4</th>
        <th>Column 5</th>
        <th>Column 6</th>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
      <tr>
        <td>Some really looong looong text that takes the whole table</td>
        <td>Value 1</td>
        <td>Value 2</td>
        <td>Value 3</td>
        <td>Value 4</td>
        <td>Value 5</td>
      </tr>
    </tbody>
  </table>
</div>
Kevin
  • 1,241
  • 7
  • 20
  • this isn't jquery datatable. I'm not using jquery at all. If you any solution in vanilla that would be great\ – ash1f Jan 30 '20 at 06:56
0

Added following styles

#table tbody tr:first-child {
  position: fixed;
}

#table tbody tr:nth-child(2) {
  padding-top: 30px;
}

#table tbody tr:not(:first-child) {  
  display: inline-block
}

.rdp-table {
  text-align: left;
  background-color: #fff;
  border-collapse: collapse;
  margin: 0 auto;
  overflow: scroll;
}

.rdp-table tbody {
  max-height: 600px;
  overflow-y: scroll;
}

.rdp-table tr {
  border: 1px solid #ccc;
}

.rdp-table tr:hover {
  background-color: #f5f5f5;
}

.rdp-table th,
.rdp-table td {
  font-weight: 600;
  line-height: 24px;
  border: 1px solid #cecece;
  min-width: 128px;
  text-align: center;
}

.rdp-table th {
  text-align: center;
  color: black;
  text-transform: uppercase;
}

#table tbody tr:first-child {
  position: fixed;
}

#table tbody tr:nth-child(2) {
  padding-top: 30px;
}

#table tbody tr:not(:first-child) {  
  display: inline-block
}
<table id="table" class="rdp-table">
  <tbody>
    <tr class="row-header">
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
      <th>Column 6</th>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
    <tr>
      <td>Some really looong looong text that takes the whole table</td>
      <td>Value 1</td>
      <td>Value 2</td>
      <td>Value 3</td>
      <td>Value 4</td>
      <td>Value 5</td>
    </tr>
  </tbody>
</table>
Aditya Bhave
  • 998
  • 1
  • 5
  • 10