使用css在打印模式下每页中的页眉和页脚

时间:2012-12-30 06:16:25

标签: css-paged-media

我有一个Web应用程序,它有一个可能超过一页的报告,我想在每个页面中打印页眉和页脚。 我找到并试试这个: Repeating a report header in each page 但它对我不起作用。我尝试使用Opera,IE9,Firefox,谷歌浏览器但是......没有。页面边距工作正常,但内容是我想要的,但它不起作用。

4 个答案:

答案 0 :(得分:19)

您可以设置position: fixed;页眉和页脚,以便在每页上重复

例如

<header class="onlyprint"><!--Content Goes Here--></header>
<footer class="onlyprint"><!--Content Goes Here--></footer>

@media screen {
    header.onlyprint, footer.onlyprint{
        display: none; /* Hide from screen */
    }
}

@media print {
    header.onlyprint {
        position: fixed; /* Display only on print page (each) */
        top: 0; /* Because it's header */
    }
    footer.onlyprint {
        position: fixed;
        bottom: 0; /* Because it's footer */
    }
}

答案 1 :(得分:6)

我非常感谢您的回复,但我之前使用过此解决方案(位置:固定),页面内容将被标题屏蔽。所以我必须使用“@page”,它仅适用于“边距”属性,“内容”不起作用,换句话说,我无法达到我想要的结果。

答案 2 :(得分:5)

目前,webkit引擎(https://bugs.webkit.org/show_bug.cgi?id=100075)中存在一个错误,导致完全错位的页脚。

答案 3 :(得分:0)

我找到了一个对我有用的解决方案,只有一个没有问题的解决方案。

在html

<table>
        <thead><tr><td>
            <div class="header-space">&nbsp;</div>
        </td></tr></thead>
        <tbody><tr><td>
            <div id="pageHost" class="content"></div>
        </td></tr></tbody>
        <tfoot><tr><td>
            <div class="footer-space">&nbsp;</div>
        </td></tr></tfoot>
    </table>
    <header id="pageHeader">
    </header>
    <footer id="pageFooter">
        Custom Footer
        <div class="numberOfPages">

        </div>
    </footer>

在CSS

            header, .header-space,
            footer, .footer-space {
                height: 100px;
                font-weight: bold;
                width: 100%;
                padding: 10pt;
                margin: 10pt 0;
            }

            header {
                position: fixed;
                top: 0;
                border-bottom: 1px solid black;
            }

            footer {
                position: fixed;
                bottom: 0;
                border-top: 1px solid black;
            }
相关问题