Critical Developers

Programmers Knowledge Base

floatheader.js - Simple jQuery Plugin for Fixed Table Headers

As there are lots of plugins to fix/float the table header and they are also included with bunch of options and features. But sometime we do not need all those features, so here in I have written a simple and a light weight plugin which floats the table header. And but obvious we all know why we need floating headers, because our user hate to scroll up and down when there are numbers numbers of records on a page.

FloatHeader is a very light-weight jQuery plugin used to fix the table header. The logic is very simple so that you can tweak as per you requirement. Only a few lines of code, thats it; what else you need.

Lets learn how to use it:-

Very first we need a table markups as shown below:-

 <table class="tblList">

. . . . . .


Add styles for table as in here:-

/* primary styles */

html, body{
  color: #000000;

/* required styles for plugin */
   overflow-x: hidden;
   overflow-y: auto;
   margin: 0 auto;

   z-index: 9999;
/* yours table styles */
    width: 100%;
    border-collapse: collapse;
    border-style: None;

.tblList thead th {
    background-color: #f7f7f7 !important;
    border-color: #cbc8c8 #cbc8c8 #e0e0e0 #cbc8c8;
    border-style: solid;
    border-width: 1px;
    font-size: 15px;
    font-weight: bold;
    height: 25px;
    padding: 4px 10px 3px;
    text-align: left;
    vertical-align: top;
.tblList tbody td {
    border-color: #cbc8c8 #cbc8c8 #e0e0e0 #cbc8c8;
    border-style: solid;
    border-width: 1px;
    font-size: 14px;
    font-weight: normal;
    padding: 4px 10px 6px;
    vertical-align: top;
.tblList tr.alt td {
    background-color: #FFFFFF !important;
.tblList tbody tr:nth-child(2n+1) {
    background-color: #F7F7F7 !important;
.tblList tbody tr:nth-child(2n) {
    background-color: #FFFFFF !important;

Add a reference of jQuery.js either using local path or using cdn.

Then Add floatheader.js plugin which is written as below:-

    // FloatHeader v1.0 - jQuery floatheader plugin
    // (c) 2015 Ram Yadav -
    // License:

    (function ($) {
        $.fn.floatheader = function (options) {
            // Default Settings
            var settings = $.extend({

            }, options);
            // Apply Settings Variables
            function fixheader(elem) {
               // Add supportive wrappers to scrollable table
               $(elem).wrap('<div class="maincontainer">');
               $("div.maincontainer").prepend('<div class="headerclone"></div>');
               $(".headerclone").find("table > tbody").remove();

               // To get same width of header while cloning
               $(elem).find("thead th").each(function(i){
               $(".headerclone").find("table thead tr th").eq(i).css({"width":$(this).width()+"px"});           

               // onscroll show/hide the cloned header
               $("div.maincontainer").bind("scroll", function () {
                   var offset = $(this).scrollTop();
                   var headerheight = $(elem).find("thead th").eq(0).outerHeight();
                   if (offset >= headerheight) {
                       $(".headerclone").css({ "top": offset + "px" });
                   else {
                       $(".headerclone").css({ "top": offset + "px" });
            return fixheader(this);
    } (jQuery));

And Now its time to call the floatheader as here below:-

<script type="text/javascript">

    $(document).ready(function () {


To view a demo click here

Comments (1) -

  • ziya

    30-03-2015 13:08:34 |

    Hey ! Thanks a lot, many time i searched about fix header but not get such a good plugin, you saved my time thanks a  lot.

    1000 Point out of 100 for this article Smile

Comments are closed