Webページ内の複数ページにまたぐテーブルをエクセルのようにヘッダーを固定してすべてのページに印刷する方法

【HTML】

固定させたいヘッダ部分を<thead></thead>タグで囲みます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<table>
<thead>
<tr>
<th>First Heading</th>
<th>Second Heading</th>
<th>Third Heading</th>
</tr>
</thead>
<tbody>
<tr>
<td>foo</td>
<td>bar</td>
<td>baz</td>
</tr>
. . .
<tr>
<td>fim</td>
<td>fam</td>
<td>fom</td>
</tr>
</tbody>
</table>
<table> <thead> <tr> <th>First Heading</th> <th>Second Heading</th> <th>Third Heading</th> </tr> </thead> <tbody> <tr> <td>foo</td> <td>bar</td> <td>baz</td> </tr> . . . <tr> <td>fim</td> <td>fam</td> <td>fom</td> </tr> </tbody> </table>
<table>
   <thead>
      <tr>
         <th>First Heading</th>
         <th>Second Heading</th>
         <th>Third Heading</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>foo</td>
         <td>bar</td>
         <td>baz</td>
      </tr>

      . . .

      <tr>
         <td>fim</td>
         <td>fam</td>
         <td>fom</td>
      </tr>
   </tbody>
</table>

【CSS】

CSSで次のように記述します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@media print {
thead {display: table-header-group;}
}
@media print { thead {display: table-header-group;} }
@media print {
   thead {display: table-header-group;}
}

【参考ページ】

http://www.terminally-incoherent.com/blog/2009/10/12/repeating-html-table-headers-on-each-printed-page/

関連記事: