Digital Colony!

Fixed Column HTML Tables

A developer will often get a requirement to query a database and return the results in an HTML table. If the number of records returned is a lot then the developer is asked to page the results (click here for the next 20 records). Paging is no fun, but it understandable that the user would not want to scroll to the point where they can't view the column headers. An ideal sitution is to return as much of the data as possible in format that the user can easily view and understand the data. Microsoft Excel allows users to fix column headers, this article explains how to do it HTML.

A Simple Solution

In HTML we can used a fixed width DIV to hold the data inside a TABLE. And we can restrict the height of the DIV to a pre-defined number of pixels. For space reasons, the examples below restrict the TABLE to 70 pixels. Setting the overflow attribute to auto will force the scrolling to take place inside the DIV. Now we can just code the column headers above the DIV in a separate TABLE. If the column header TABLE and the DIV have the same width it will appear as though the column headers are fixed.
<table width="400">
<tr>
<th>Name</th><th>City</th><th>1999 Sales</th><th>2000 Sales</th><th>2001 Sales</th>
<tr>
</table>

<div style="width:400; height:100; overflow:auto;border:" id="dataList">
<table width="380">
<!-- Add records here -->
</table>
</div>

Example 1

NameCity1999 Sales2000 Sales2001 Sales
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000

Getting the Columns Straight

In the above example the columns are a little crooked. They don't exactly line up. I tried use javascript to calculate the width of the data cells in order to draw fixed width column headers, but I couldn't get it to work. If anyone has been able to to do this please email me the code. In order for the column headers to line up straight, I did it the old-fashioned way: I hard coded the width of the table cells to match the width of the column headers.

Example 2

This is the same table with fixed width cells.
NameCity1999 Sales2000 Sales2001 Sales
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000

More Tips

Sometimes sizing everything perfectly can be difficult, especially if the data varies in length. Here are a few more tips to help achieve an Excel-like table in HTML.

table-layout:fixed - Dianliang Zhu alerted me to this CSS tag. By adding STYLE="table-layout:fixed;" to our table we can contain the browser cell to the width we define. However, if we don't define our cell widths, it will use the width of the first cells to determine the table layout. This is an excellent tag to use, so be sure to define the cell widths.

Fixed-Width Fonts - Counting characters can be a pain, especially when the size of each character varies in width. To eliminate this possibility consider using a fixed-width (aka Monospace) font such as Courier for the table.

Server-Side Data Trimming - To prevent long pieces of data from pushing out or streching a cell, perform some server-siding trimming. Do you need the full company name or would the first 30 characters be enough? (EX: companyName = Left(companyname,30))

Example 3 - Using More Tips

NameCity1999 Sales2000 Sales2001 Sales
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000
JoeAtlanta$400,000$450,000$430,000
SueMemphis$500,000$550,000$455,000
MattNYC$300,000$350,000$330,000
LouAkron$200,000$150,000$130,000
AnnAustin$400,000$450,000$430,000

Last Words

As expected, not every browser supports the overflow feature. The ideal use of this would be for a corporate Intranet where users were restricted to a one standards-compliant browser.

This article was first written in 2002. For a different solution see Cross-browser scrolling tbody.

Labels: ,

AddThis Social Bookmark Button

11 Comments:

Anonymous Anonymous said...

Excellent tip for creating fixed-width tables. Worked for me :-)

4/18/2007 1:44 AM

Anonymous Anonymous said...

I was hitting my head this helped me Thanks:-)
Kamal

10/24/2007 10:06 PM

Anonymous raja said...

style="table-layout:fixed". Thank you for the idea.

12/07/2007 2:27 PM

Anonymous raja.B said...

Nice tips. Thanks a bunch.

12/07/2007 2:28 PM

Anonymous Rahul K. said...

style="table-layout:fixed"
gr8....
I was searching for some like this for last 2 weeks..
Thanks a lot

12/22/2007 6:00 PM

Anonymous Abhay said...

Thx a Lot for this tip. It helped a lot to me.

1/16/2008 2:46 AM

Blogger Senhor said...

Thanks a lot,man! This is a simple solution for a BIG problem! Thanks a lot!

2/10/2008 2:23 AM

Anonymous Anonymous said...

Thanks! :)

5/07/2008 7:44 PM

Anonymous Erik said...

Looks good....but what if i want to align the cells from upper and lower tables.
what if either one of the names or values becomes larger, it would ruin the alignment.
I have tried the "fixed" idea, but IE ruins it...

9/21/2008 5:52 AM

Blogger MAS said...

Erik - it has been 6 years since I looked at this code. Browsers have changed. I hope it gets you closer to the layout you end up using.

9/21/2008 8:14 AM

Anonymous Erik said...

Don't worry, i found a way.....got it working...
thanx...

10/02/2008 2:43 PM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.