table with fixed header and sidebar

Table with fixed header and sidebar

Hello friends, here I am going to share table with fixed header and sidebar. Idea to develop this solution comes in mind when I was working on web project where I need to fixed the header and sidebar for the table. I needed a way to create a solution or new layout of table where header is fixed and fixed flexible sidebar that can adjust its width according to content. Here in this post I will share how I have created this layout using little help of javascript and advance CSS properties.

There are few key features of layout we are going to create:
  1. Sticky Header
  2. Sticky Sidebar
  3. Scrollable table body both vertical and horizontal.
Let’s start the work:

First of all we will create our normal html table structure where we have header and table body section, code as follow.

Html:

It is normal table structure nothing special coding in it. Now we will right our css code to get its layout and as well as special edit for table by CSS coding to provide it fancy layout and desired seperate header and sidebar.

<div class="container">
    <div class="row">
      <div class="col-12">
        <table class="table  table-striped">
          <thead>
            <tr>
              <th>User Info<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
              <th>Town<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
              <th>County<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
              <th>Age<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
              <th>Profession<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
              <th>Anual Income<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg">
              </th>
              <th>Matital Status<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg">
              </th>
              <th>Children<img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/sort.svg"></th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-1.svg"> John Smith
              </td>
              <td>Macelsfield</td>
              <td>Cheshire</td>
              <td>52</td>
              <td>Brewer</td>
              <td>£47,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-2.svg"> Jenny Jones
              </td>
              <td>Threlkeld</td>
              <td>Cumbria</td>
              <td>34</td>
              <td>Shepherdess</td>
              <td>£28,000</td>
              <td>Single</td>
              <td>0</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-3.svg"> Peter
                Frampton</td>
              <td>Avebury</td>
              <td>Wiltshire</td>
              <td>57</td>
              <td>Musician</td>
              <td>£124,000</td>
              <td>Married</td>
              <td>4</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-4.svg"> Simon King
              </td>
              <td>Malvern</td>
              <td>Worchestershire</td>
              <td>48</td>
              <td>Naturalist</td>
              <td>£65,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-1.svg"> John Smith
              </td>
              <td>Macelsfield</td>
              <td>Cheshire</td>
              <td>52</td>
              <td>Brewer</td>
              <td>£47,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-2.svg"> Jenny Jones
              </td>
              <td>Threlkeld</td>
              <td>Cumbria</td>
              <td>34</td>
              <td>Shepherdess</td>
              <td>£28,000</td>
              <td>Single</td>
              <td>0</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-3.svg"> Peter
                Frampton</td>
              <td>Avebury</td>
              <td>Wiltshire</td>
              <td>57</td>
              <td>Musician</td>
              <td>£124,000</td>
              <td>Married</td>
              <td>4</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-4.svg"> Simon King
              </td>
              <td>Malvern</td>
              <td>Worchestershire</td>
              <td>48</td>
              <td>Naturalist</td>
              <td>£65,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-1.svg"> John Smith
              </td>
              <td>Macelsfield</td>
              <td>Cheshire</td>
              <td>52</td>
              <td>Brewer</td>
              <td>£47,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-2.svg"> Jenny Jones
              </td>
              <td>Threlkeld</td>
              <td>Cumbria</td>
              <td>34</td>
              <td>Shepherdess</td>
              <td>£28,000</td>
              <td>Single</td>
              <td>0</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-3.svg"> Peter
                Frampton</td>
              <td>Avebury</td>
              <td>Wiltshire</td>
              <td>57</td>
              <td>Musician</td>
              <td>£124,000</td>
              <td>Married</td>
              <td>4</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-4.svg"> Simon King
              </td>
              <td>Malvern</td>
              <td>Worchestershire</td>
              <td>48</td>
              <td>Naturalist</td>
              <td>£65,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-1.svg"> John Smith
              </td>
              <td>Macelsfield</td>
              <td>Cheshire</td>
              <td>52</td>
              <td>Brewer</td>
              <td>£47,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-2.svg"> Jenny Jones
              </td>
              <td>Threlkeld</td>
              <td>Cumbria</td>
              <td>34</td>
              <td>Shepherdess</td>
              <td>£28,000</td>
              <td>Single</td>
              <td>0</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-3.svg"> Peter
                Frampton</td>
              <td>Avebury</td>
              <td>Wiltshire</td>
              <td>57</td>
              <td>Musician</td>
              <td>£124,000</td>
              <td>Married</td>
              <td>4</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-4.svg"> Simon King
              </td>
              <td>Malvern</td>
              <td>Worchestershire</td>
              <td>48</td>
              <td>Naturalist</td>
              <td>£65,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-1.svg"> John Smith
              </td>
              <td>Macelsfield</td>
              <td>Cheshire</td>
              <td>52</td>
              <td>Brewer</td>
              <td>£47,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-2.svg"> Jenny Jones
              </td>
              <td>Threlkeld</td>
              <td>Cumbria</td>
              <td>34</td>
              <td>Shepherdess</td>
              <td>£28,000</td>
              <td>Single</td>
              <td>0</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-3.svg"> Peter
                Frampton</td>
              <td>Avebury</td>
              <td>Wiltshire</td>
              <td>57</td>
              <td>Musician</td>
              <td>£124,000</td>
              <td>Married</td>
              <td>4</td>
            </tr>
            <tr>
              <td><img src="http://css3transition.com/demo/toggle-side-menu/stylesheet/images/person-4.svg"> Simon King
              </td>
              <td>Malvern</td>
              <td>Worchestershire</td>
              <td>48</td>
              <td>Naturalist</td>
              <td>£65,000</td>
              <td>Married</td>
              <td>2</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>

Css Code:

In this css code you can see I have taken first Element th and td in both thead and tbody in table. Make these element display: block; in place display:table-cell; and assigned a min-width:300px;. along with this apply display:block; css to thead and tbody section too so that we can apply scroll on that. rest is normal css to beautiy this table.

@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap');

    body {
      font-family: 'Source Sans Pro', sans-serif;
      background: url(https://image.freepik.com/free-photo/blurred-business-workplace-background_23-2148187123.jpg);
      background-size: cover;
      background-repeat: no-repeat;
      height: 100vh;
    }

    table {
      position: relative;
      width: 100%;
      table-layout: fixed;
      overflow: hidden;
      border-collapse: collapse;
      margin-top: 80px;
      background: rgba(255, 255, 255, 0.8);
    }

    thead {
      position: relative;
      display: block;
      overflow: visible;
      width: 100%;
    }

    thead th {
      background-color: #de52ff;
      min-width: 250px;
      padding: 10px 15px !important;
      color: #ffffff;
      font-size: 1em;
      border: none !important;
      font-weight: 400;
    }

    thead th img {
      height: 16px;
      width: 16px;
      margin-left: 5px;
    }

    thead th:nth-child(1) {
      position: relative;
      display: block;
      background-color: #de52ff;
      padding: 10px 20px;
      color: #ffffff;
      box-shadow: 0 1px 2px #efefef;
      min-width: 300px;
    }

    tbody {
      position: relative;
      display: block;
      height: 550px;
      overflow: scroll;
      width: 100%;
    }

    tbody td {
      min-width: 250px;
      padding: 10px 15px !important;
      color: #5f5f5f;
      font-size: 0.9em;
      border: none !important;
      vertical-align: middle !important;
    }

    tbody tr td:nth-child(1) {
      position: relative;
      display: block;
      background-color: #f5f9fb;
      border-bottom: 1px;
      border-right: 1px;
      box-shadow: 0 1px 2px #efefef;
      min-width: 300px;
    }

    tbody tr td:nth-child(1) img {
      width: 50px;
      height: 50px;
      margin-right: 5px;
    }

    table tr:nth-child(even) td:first-child {
      background: #f5dcff;
    }

    table tr:nth-child(odd) td:first-child {
      background: #f0cbff;
    }

Javascript:

Here I am applying scroll to tbody by simple javascript code. It is very simple and easy to understand as we know table body can not be scroll without any special edit so I am taking help of javascript and css as show in this example.

    $(document).ready(function () {
      $('tbody').scroll(function (e) {
        $('thead').css("left", -$("tbody").scrollLeft()); //fix the thead relative to the body scrolling
        $('thead th:nth-child(1)').css("left", $("tbody").scrollLeft()); //fix the first cell of the header
        $('tbody td:nth-child(1)').css("left", $("tbody").scrollLeft()); //fix the first column of tdbody
      });
    });

Leave a Reply

Your email address will not be published. Required fields are marked *