Bilal
Bilal

Reputation: 2740

Keep table header visible without showing scrollbars in table body

I am making a page with tabular data is scrollable with main page scrollbar but with keeping table header visible. There's a page header that sticks on top and pagination area that sticks on bottom on the page.

I gave it a try here.

var dataHeader = document.querySelector('.data thead');

var scrollValue = 50;

window.onscroll = function () {
    if (document.body.scrollTop > scrollValue || document.documentElement.scrollTop > scrollValue) {
        dataHeader.className = "fixed";
    } else {
        dataHeader.className = "";
    }
};
.main {
  position: relative;
  top: 0;
  width: 700px;
  margin: 0 auto;
}
.header {
  position: fixed;
  top: 0;
  background-color: #abc;
  width: 700px;
  z-index: 10;
}

.content {
  position: relative;
  top: 60px;
  background: #cfe;
}

.content-header {
  color: blue;
  padding-top: 10px;
}

.data {
  position: relative;
  margin-bottom: 18px;
}

.data table {
  border-collapse: collapse;
  width: 700px;
}

.data thead {
  background: grey;
  width: 700px;
  display: table-header-group;
}

.data tbody {
  width: 700px;
}

.data thead.fixed {
  position: fixed;
  top: 80px;
}

.data thead.fixed th {
  width: 90px;
}

.paging {
  position: absolute;
  bottom: 0;
  background-color: black;
  color: white;
  width: 700px;
}

.column {
  padding: 2;
  width: 80px;
}
<div class="main">

  <div class="header">
    <h1>Super Page</h1>
  </div>

  <div class="content">

    <div class="content-header">
      <h2>Beautiful Content</h2>
    </div>

    <div class="data">
      <table>
        <colgroup>
          <col class="column">
          <col class="column">
          <col class="column">
        </colgroup>
        <thead>
          <tr>
            <th class="column">Sr</th>
            <th class="column">City</th>
            <th class="column">Country</th>
          </tr>
          <thead>
            <tbody>
              <tr>
                <td>1</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>2</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>3</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>4</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>5</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>6</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>7</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>8</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>9</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>10</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>11</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>12</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>13</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>14</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>15</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>16</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>17</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>18</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>19</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>20</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>21</td>
                <td>Prague</td>
                <td>Czech Republic</td>
              </tr>
            </tbody>
      </table>
    </div>
    <!-- /.data -->

    <div class="paging">
      Displaying 10 of 100 records
    </div>
  </div>
  <!-- /.content -->

</div>
<!-- /.main -->

The only problem is when I scroll down, table header kind of collapses and does not remain aligned with table body's columns. How Can I fix it?

Upvotes: 0

Views: 42

Answers (1)

Mers
Mers

Reputation: 734

You can adjust your .data thead.fixed th rule to width: calc(700px/3); like so:

var dataHeader = document.querySelector('.data thead');

var scrollValue = 50;

window.onscroll = function () {
    if (document.body.scrollTop > scrollValue || document.documentElement.scrollTop > scrollValue) {
        dataHeader.className = "fixed";
    } else {
        dataHeader.className = "";
    }
};
.main {
  position: relative;
  top: 0;
  width: 700px;
  margin: 0 auto;
}
.header {
  position: fixed;
  top: 0;
  background-color: #abc;
  width: 700px;
  z-index: 10;
}

.content {
  position: relative;
  top: 60px;
  background: #cfe;
}

.content-header {
  color: blue;
  padding-top: 10px;
}

.data {
  position: relative;
  margin-bottom: 18px;
}

.data table {
  border-collapse: collapse;
  width: 700px;
}

.data thead {
  background: grey;
  width: 700px;
  display: table-header-group;
}

.data tbody {
  width: 700px;
}

.data thead.fixed {
  position: fixed;
  top: 80px;
}

.data thead.fixed th {
  width: calc(700px/3);  
}

.paging {
  position: absolute;
  bottom: 0;
  background-color: black;
  color: white;
  width: 700px;
}

.column {
  padding: 2;
  width: 80px;
<div class="main">

  <div class="header">
    <h1>Super Page</h1>
  </div>

  <div class="content">

    <div class="content-header">
      <h2>Beautiful Content</h2>
    </div>

    <div class="data">
      <table>
        <colgroup>
          <col class="column">
          <col class="column">
          <col class="column">
        </colgroup>
        <thead>
          <tr>
            <th class="column">Sr</th>
            <th class="column">City</th>
            <th class="column">Country</th>
          </tr>
          <thead>
            <tbody>
              <tr>
                <td>1</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>2</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>3</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>4</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>5</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>6</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>7</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>8</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>9</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>10</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>11</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>12</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>13</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>14</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>15</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>16</td>
                <td>Amsterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>17</td>
                <td>Lahore</td>
                <td>Pakistan</td>
              </tr>
              <tr>
                <td>18</td>
                <td>Doha</td>
                <td>Qatar</td>
              </tr>
              <tr>
                <td>19</td>
                <td>Mumbai</td>
                <td>India</td>
              </tr>
              <tr>
                <td>20</td>
                <td>Rotterdam</td>
                <td>Netherlands</td>
              </tr>
              <tr>
                <td>21</td>
                <td>Prague</td>
                <td>Czech Republic</td>
              </tr>
            </tbody>
      </table>
    </div>
    <!-- /.data -->

    <div class="paging">
      Displaying 10 of 100 records
    </div>
  </div>
  <!-- /.content -->

</div>
<!-- /.main -->

Upvotes: 2

Related Questions