我有一个元素需要打印一个包含许多行的 HTML 表格。我想在每个页面的顶部显示 thead
部分。我使用的是 IE11 浏览器。
<style>
@media print {
#Header {
display: table-header-group;
}
table {
page-break-inside: auto;
}
tr {
page-break-inside: auto;
position: static;
}
}
</style>
<div class="tab-pane active " id="template">
<table>
<thead>
<div id="Header">
<table style="width: 100%; table-layout: fixed;" id="tbl_1" class="table table-bordered">
<tbody>
<tr id="1">
<td style="height: 18px; border:solid; " ></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
</tr>
<tr id="2">
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
</tr>
</tbody>
</table>
</div>
</thead>
<tbody>
<div id="contents">
<!--more then 800 rows in table-->
</div>
</tbody>
</table>
</div>
最佳答案
您可以通过实现 PHP 来实现这一目标。当我开始使用 PHP 时,我的导师只是让我们使用 PHP 创建一个“Web 模板”。此"template"将确保页面相似并具有一致的页眉、导航、页脚等。
您可以创建一个文件 (thead.php),其中包含您要重复的所有代码。我假设您想重复 <thead></thead>
之间的内容
(thead.php)
<thead>
<div id="Header">
<table style="width: 100%; table-layout: fixed;" id="tbl_1" class="table table-bordered">
<tbody>
<tr id="1">
<td style="height: 18px; border:solid; " ></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
</tr>
<tr id="2">
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
<td style="height: 18px; border:solid; "></td>
</tr>
</tbody>
</table>
</div>
</thead>
(你的文件.php)
<style>
@media print {
#Header {
display: table-header-group;
}
table {
page-break-inside: auto;
}
tr {
page-break-inside: auto;
position: static;
}
}
</style>
<div class="tab-pane active " id="template">
<table>
<?php include 'thead.php'; ?>
<tbody>
<div id="contents">
<!--more then 800 rows in table-->
</div>
</tbody>
</table>
</div>
这将导致您必须将文件类型更改为 .php 并且您将不得不使用本地服务器进行测试,因为浏览器无法处理 .php 文件。我个人使用 XAMPP,但我确信还有许多其他选择。
如果您还不知道任何 php,它所做的基本上是根据通过您的 .PHP 文件中的脚本提供给它的信息为您编写一个 HTML 文件。当文件通过服务器运行后到达浏览器时,它只是可以处理和显示的简单 HTML 代码。因此,如果您按照我向您展示的方式进行操作,到达浏览器的最终文件不会有任何不同,只是您需要编写的代码更少。
关于html - 打印时使用 css 在每页上重复页眉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079512/