Click here to Skip to main content
Click here to Skip to main content

Freeze Panes like Excel

, 1 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Freeze panes in an HTML table, cross browser using JavaScript.


The script described in this article simply hides/unhides columns or rows using scrolling buttons, creating an Excel-like effect.


There are already a few scripts around that demonstrate how to freeze an HTML table top row and first column.

Most of these scripts are specific to Internet Explorer, while a couple I have tried are cross browser compatible using CSS, but require the columns to be set to a specific width... for example, all column widths must be set to 100px wide.

I found this not to be ideal because the columns hide any content that is wider than 100px and there is no way of adjusting the column widths to view the hidden data via an onscreen control....not ideal for dynamically created data from a database.

Another solution I found was quite good but required a very large and complex amount of JavaScript code.

Using the code

The script simply hides/unhides columns or rows using scrolling buttons:

<button title="Up" onmousedown="upp();" onmouseup="upp(1);">Up</button>
<button title="Left" onmousedown="left();" 
<button title="Right" onmousedown="right();" 
<button title="Down" onmousedown="down();" onmouseup="down(1);">Dn</button>

Naturally, images can easily be used for a more attractive display.

Place the JavaScript function in either the head or body of your page. The function setUp() initializes the table the first time one of the buttons is clicked. Make sure to modify:


to match the ID of your table if it is not "t1". The script could be better optimised, but for demonstration, the code is easy enough to follow.

  var myRow=1;
  var myCol=1;
  var myTable;
  var noRows;
  var myCells,ID;
function setUp(){
     myCells = myTable.rows[0].cells.length;

    for( var x = 0; x < myTable.rows[0].cells.length; x++ ) {


function right(up){

        for( var x = 0; x < noRows; x++ ) {
        if(myCol >1){myCol--;}

        ID = window.setTimeout('right()',100);
function left(up){

        for( var x = 0; x < noRows; x++ ) {
        ID = window.setTimeout('left()',100);
function down(up){

            myRow++    ;
            ID = window.setTimeout('down()',100);
function upp(up){

        if(myRow >1){myRow--;}
        ID = window.setTimeout('upp()',100);

See a working Freeze Pains Demo here.

Here is an updated version for better layout within a scrollable div, using custom horizontal and verticle scrollbars: demo.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


Australia Australia
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberMember 961177414-Feb-13 7:11 
QuestionTook your code and added some Jquery to it PinmemberMember 961177414-Feb-13 7:11 
AnswerRe: Took your code and added some Jquery to it PinmemberMember 1088420814-Jun-14 4:12 
GeneralMy vote of 5 PinmemberJonAtkins5720-Oct-12 11:31 
QuestionJavascript Pinmemberursatish20-Sep-12 20:17 
GeneralTable inside table problem Pinmemberchadok17-Jun-11 4:43 
Questionfreeze pane error PinmemberLim Tjie Siong, Indo10-Mar-11 1:12 
Generalcss help needed Pinmemberwebber1234563-Jan-11 9:38 
GeneralRe: css help needed PinmemberWill663-Jan-11 18:59 
GeneralRe: css help needed Pinmemberwebber1234569-Jan-11 11:45 
GeneralRe: css help needed PinmemberWill669-Jan-11 14:32 
GeneralRe: css help needed Pinmemberwebber12345610-Jan-11 12:11 
GeneralMy vote of 3 PinmemberNawaz M11-Oct-10 12:18 
Generalhorizontal scroll working only once pls help Pinmembersnigdha123412-Aug-10 2:24 
GeneralRe: horizontal scroll working only once pls help PinmemberWill6612-Aug-10 4:16 
GeneralRe: horizontal scroll working only once pls help Pinmembersnigdha123413-Aug-10 0:09 
GeneralProblem when cells are merged Pinmemberamit12121242-Aug-10 22:45 
GeneralRe: Problem when cells are merged Pinmemberlubos190221-Sep-11 20:29 
GeneralUpdated: now in custom scrollable div PinmemberWill661-Mar-08 4:57 
GeneralRe: Updated: now in custom scrollable div PinmemberMarc Leger1-Mar-08 10:52 
AnswerMultiple Table Solution PinmemberEric Carter28-Feb-08 12:23 
GeneralRe: Multiple Table Solution PinmemberWill661-Mar-08 4:20 
GeneralAnother option (using JQuery) Pinmemberrubensr22-Aug-07 17:33 
GeneralPrint large html table PinmemberElyons0321-Aug-07 16:11 
GeneralTry CSS Pinmembermeteorbites20-Aug-07 21:22 
GeneralRe: Try CSS PinmemberWill6620-Aug-07 22:35 
GeneralRe: Try CSS PinmemberLouisa Chen21-Aug-07 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 1 Mar 2008
Article Copyright 2007 by Will66
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid