Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's something that <em>kind of</em> works (quickly tested in FF 3.5, Chrome 3, and IE 8) that you might be able to tailor to your needs.</p> <p>It uses absolute positioning of the <code>thead</code>. When the <code>thead</code> becomes absolutely positioned, this basically removes it from the original flow of the <code>table</code> and the widths of all the <code>tbody</code> <code>td</code>s adjust accordingly. So you get ugly behavior if you don't specify column widths or if any column's header is wider than any other cell in that column.</p> <p><strong>CSS</strong></p> <pre><code>#Grid thead.Fixed { position: absolute; } </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;table id="Grid"&gt; &lt;thead&gt; &lt;tr&gt; &lt;td&gt;A&lt;/td&gt; &lt;td&gt;B&lt;/td&gt; &lt;td&gt;C&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt; &lt;!-- etc. --&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p><strong>jQuery</strong></p> <pre><code>$(function() { var table = $("#Grid"); $(window).scroll(function() { var windowTop = $(window).scrollTop(); if (windowTop &gt; table.offset().top) { $("thead", table).addClass("Fixed").css("top", windowTop); } else { $("thead", table).removeClass("Fixed"); } }); }); </code></pre> <p>I noticed that it does not work in IE unless you specify a strict XHTML doctype:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt; &lt;!-- etc. --&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload