csshtml-tablefixed-header-tables

How to Fixed Header in html table with have multiple row header with row-span and coll-span


i am setting up a html table which has header <thead> with 2 <tr> which includes rowspan and colspan and i want to fixed that <thead> part in my html table

<style type="text/css">
    table#customers {
      display: table;
      font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
      border-collapse: collapse;
      width: 100%;
    }

    #customers td, #customers th {
      border: 1px solid #ddd;
      padding: 8px;
    }

    #customers tr:nth-child(even){background-color: #f2f2f2;}

    #customers tr:hover {background-color: #ddd;}

    #customers th {
      padding-top: 12px;
      padding-bottom: 12px;
      text-align: left;
      background-color: #2C3B49;
      color: white;
    }

</style>

here the table

<table id="customers">
    <thead>
        <tr>
           <th rowspan="2">rowspan="2"</th>
           <th rowspan="2">
              <center>rowspan="2"</center>
           </th>
           <th colspan="4">
              <center>colspan="4"</center>
           </th>
           <th colspan="4">
              <center>colspan="4"</center>
           </th>
           <th colspan="4">
              <center>colspan="4"</center>
           </th>
        </tr>
        <tr>
           <th>
              <center>text 1</center>
           </th>
           <th>
              <center>text 2</center>
           </th>
           <th>
              <center>text 3</center>
           </th>
           <th>
              <center>text 4</center>
           </th>
           <th>
              <center>text 1</center>
           </th>
           <th>
              <center>text 2</center>
           </th>
           <th>
              <center>text 3</center>
           </th>
           <th>
              <center>text 4</center>
           </th>
           <th>
              <center>text 1</center>
           </th>
           <th>
              <center>text 2</center>
           </th>
           <th>
              <center>text 3</center>
           </th>
           <th>
              <center>text 4</center>
           </th>
        </tr>
    </thead>
    <tbody>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
        <tr>
         <td>test</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
         <td>000</td>
        </tr>
    </tbody>
</table>

here the screen shot what i made https://prnt.sc/oz2p1f

what i want is, the whole <thead> tag will fixed with both scroll, i tried a so many ways but i didn't get any of perfect example which were helped me.

here is anyone can help me ? for this Thank you in advance


Solution

  • Add this <div> around your table:

    <div class="tableFixHead"></div>
    

    Add this CSS:

    .tableFixHead { overflow-y: auto; height: 400px; }
    

    And this jQuery:

    var $th = $('.tableFixHead').find('thead th')
    $('.tableFixHead').on('scroll', function() {
      $th.css('transform', 'translateY('+ this.scrollTop +'px)');
    });
    

    JSFiddle: https://jsfiddle.net/mvoisard/unLx7h1z/8/