NickMalloy
Programmer
Is there a way to make a table with a static header that will work in IE and doesn't use Javascript? How would you do this? I tried experiementing with <thead> and <tbody> tags.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<!-- ***************************** HEAD **************************** -->
<head><title>Non-scrolling Table Headers</title>
<style type="text/css">
th {text-align:left; font-weight:normal}
td {width:28%}
div {height:200px; overflow:scroll; background:#ff9}
.width {width:400px}
</style>
<!--[if IE]>
<style type="text/css">
div {overflow:hidden}
div {overflow-y:scroll}
</style>
<![endif]-->
</head>
<!-- ***************************** BODY **************************** -->
<body><h3>Non-scrolling Table Headers</h3>
<table class="width" border="1">
<tr><td>Station</td><td>Date</td><td>Status</td><th>Num.</th></tr>
</table><div class="width">
<table class="width" border="1">
<tr><td>KABC</td><td>09/12/2002</td><td>Submitted</td><th>0</th></tr>
<tr><td>KCBS</td><td>09/11/2002</td><td>Lockdown</td><th>2</th></tr>
<tr><td>WFLA</td><td>09/11/2002</td><td>Submitted</td><th>1</th></tr>
<tr><td>WTSP</td><td>09/15/2002</td><td>In-Progress</td><th>10</th></tr>
<tr><td>WROC</td><td>10/11/2002</td><td>Submitted</td><th>12</th></tr>
<tr><td>WPPP</td><td>09/16/2002</td><td>In-Progress</td><th>0</th></tr>
<tr><td>WRRR</td><td>09/06/2002</td><td>Submitted</td><th>5</th></tr>
<tr><td>WTTT</td><td>09/21/2002</td><td>In-Progress</td><th>0</th></tr>
<tr><td>W000</td><td>11/11/2002</td><td>Submitted</td><th>7</th></tr>
<tr><td>KABC</td><td>10/01/2002</td><td>Submitted</td><th>10</th></tr>
<tr><td>KCBS</td><td>10/18/2002</td><td>Lockdown</td><th>2</th></tr>
<tr><td>WFLA</td><td>10/18/2002</td><td>Submitted</td><th>1</th></tr>
<tr><td>WTSP</td><td>10/19/2002</td><td>In-Progress</td><th>0</th></tr>
</table></div></body></html>
What is an isn't a "hack" is a pretty subjective judgement. Code that I like is OK, code that I don't like is "full of hacks".I think the [if IE] code is not in the normal sense a hack...
* html div {overflow:hidden;
overflow-y:scroll}
I have tended to stay away from more esoteric aspects of CSS because of compatibility issues but I thought the, [if IE], thing seemed real promising:AFAIK it only works in (X)HTML
* html div {overflow:hidden;
overflow-y:scroll}
<script type="text/javascript">
<!--
/* [URL unfurl="true"]http://www.alistapart.com/articles/zebratables/[/URL] */
function removeClassName (elem, className) {
elem.className = elem.className.replace(className, "").trim();
}
function addCSSClass (elem, className) {
removeClassName (elem, className);
elem.className = (elem.className + " " + className).trim();
}
String.prototype.trim = function() {
return this.replace( /^\s+|\s+$/, "" );
}
function stripedTable() {
if (document.getElementById && document.getElementsByTagName) {
var allTables = document.getElementsByTagName('table');
if (!allTables) { return; }
for (var i = 0; i < allTables.length; i++) {
if (allTables[i].className.match(/[\w\s ]*scrollTable[\w\s ]*/)) {
var trs = allTables[i].getElementsByTagName("tr");
for (var j = 0; j < trs.length; j++) {
removeClassName(trs[j], 'alternateRow');
addCSSClass(trs[j], 'normalRow');
}
for (var k = 0; k < trs.length; k += 2) {
removeClassName(trs[k], 'normalRow');
addCSSClass(trs[k], 'alternateRow');
}
}
}
}
}
window.onload = function() { stripedTable(); }
-->
</script>