Email This Post Email This Post Print This Post Print This Post

Fixed Table Head

Thu, Jul 3, 2008 – 5:12 am

Scrollable Table Body with a Fixed Table Heading
Tested to work in Firefox 3 and Internet Explorer 7. Safari users may want to check out Floating Table Titles

There is no HTML or CSS standard tag or property that lets you freeze the title row of a table in place while allowing the body to scroll up and down.

Like this:

Title #1 Title #2…. Title #3……. #4
Item 1……….. Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8

In response to this I created the “FixedTableHead” CSS class.

I wanted a CSS solution that was easy to use. Other solutions I ran across on the web tended to be inflexible and hard to implement in real world situations. They had you hard coding table widths and heights in multiple locations in the CSS. This can be a real pain if you want to use it for multiple tables that vary in size.

“FixedTableHead” handles multiple tables of different sizes on the same page. I attempted to keep the HTML mark-up as simple as possible. All you need do is to assign a single CSS class name to a DIV that surrounds your table then assign a height you want the table to be as an embedded style. That’s it.

Example:


<div class="FixedTableHead">
	<table style="height:100px;">
		<thead>
			...
		</thead>
		<tbody>
			...
		</tbody>
	</table>
</div>

This has been tested to work in IE7, Firefox 2 and Firefox 3. As long as the table uses a THEAD and a TBODY then the header rows will remain fixed in place while the content in the table body scrolls up or down.

Height: 75px; Width: Nothing - What ever the content sizes it to

Title #1 Title #2…. Title #3……. #4
Item 1……….. Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8

Height: 100px; Width: 400px;

Title #1 Title #2…. Title #3……. #4
Item 1……….. Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8

Height: 200px; Width:600px;

Title #1 Title #2…. Title #3……. #4
Item 1……….. Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8

Height: 60px; Width:200px;

Title #1 Title #2…. Title #3……. #4
Item 1……….. Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8
Item 1 Item 2 Item 3 Item 4
Itemb5 Item 6 Item 7 Item 8

CSS Code:


<style type='text/css'>

/* DIV container around table to constrict the height for IE (IE ignores the tbody height style) */
div.FixedTableHead {
	overflow-y:auto;
	margin: 0px;
	padding: 0px;
	border: expression( '1px solid black');

	/* this fixes IE so container width is same as table width */
	width: expression( (this.childNodes[0].clientWidth + 17) + 'px' );

	/* This fixes IE so the container height is table height plus the height of the header */
	height: expression( (parseInt(this.childNodes[0].style.height) +
						 this.childNodes[0].childNodes[1].offsetTop + 1) +'px' );
}

/* Get rid of table cellspacing */
div.FixedTableHead table {
	border-spacing: 0px;
	border-collapse: collapse;
}

/* Get rid of table cellspacing */
div.FixedTableHead table td {
	margin: 0px;
}

/* Scrollable Content */
.FixedTableHead table tbody {
	height:100%;
	overflow-x:hidden;
	overflow-y:auto;
	border: 1px solid black;
}

.FixedTableHead table tbody tr {
	height: auto;
	white-space: nowrap;
}

/* Prevent Mozilla scrollbar from hiding right-most cell content */
.FixedTableHead table tbody tr td:last-child {
	padding-right: 20px;

}

/* Fixed Header for IE (firefox uses the tbody overflow assignment, which is ignored by IE)   */
/* Note: In IE an element with a position property set to relative and is a child of         */
/* an element that has an overflow property set, the relative value translates into fixed.    */
.FixedTableHead table thead tr {
	position: relative;
	height: auto;
	/* this fixes IE header jumping bug when mousing over rows in the tbody */
	top: expression( this.parentNode.parentNode.parentNode.scrollTop + 'px' );
}

/* Formatting Header Row */
.FixedTableHead table thead tr td {
	color: white;
	background-color: #303433;
}
</style>
  1. 7 Responses to “Fixed Table Head”

  2. Hey - looks looks useful, and very well presented. Thanks

    (Just trying to get it wokring in my code, and some very odd things are happening at the moment, think Ive got CSS issues right now…)

    By Garfy on Jul 24, 2008

  3. Thanks,

    I’ve not written it up as well, but there is another approach I was looking at that involved having the titles of the page float down as the user scrolls down the page.

    You can find a preliminary demo page of this here:
    http://www.stansight.com/TableFloaterTitle.html

    By Stan Slaughter on Jul 24, 2008

  4. Hi , It set the header off the table?

    By Vitor Yoshida on Jul 29, 2008

  5. Vitor,

    Not clear on exactly what you are trying to say, but the script on this page will take the rows between the THEAD tags and keep them fixed in place.

    By Stan Slaughter on Jul 29, 2008

  6. Great stuff.
    Do you have a way of locking a column (usually the first column)?
    Thanks
    Simon

    By simon on Aug 6, 2008

  7. I haven’t come up with a cross browser solution to column locking, but an Internet Explorer only solution by Brett Merkey can be found here: http://home.tampabay.rr.com/bmerkey/examples/locked-column-csv.html

    I’ve found his solution useful, but a little harder to implement than it could be, and of course, not Firefox or Safari friendly.

    BUT - If all your users have IE (like on a corporate intranet) then it is a solution to your problem.

    I’ll start looking into the column locking issue myself. If I figure out a solution I’ll publish the results on this site.

    By Stan Slaughter on Aug 8, 2008

  1. 1 Trackback(s)

  2. Jul 29, 2008: StanSight » Blog Archive » Floating Table Titles

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word