CSS page x of y for @media print

前端 未结 2 1336
难免孤独
难免孤独 2021-01-12 09:06

I\'ll preface this question by saying I know this question has been asked before, but all the answers I can find for these appear to reference an obsolete solution that no l

相关标签:
2条回答
  • 2021-01-12 09:47

    As of CSS3 you can specify counters in the @page rule. Here is an example:

    @page { counter-increment: page }
    

    The above rule instructs the layout engine to create a counter called "page" (it is called page by convention, it can be anything). This counter is incremented for each page. As with any counter, you can then use the current value of the counter anywhere in the document

    For example with this CSS rule:

    #pageNumber { content: counter(page) }
    

    and this piece of HTML:

    <span id="pageNumber"></span>
    

    You can use the current page number counter as content in the HTML document. You can even go further. Say you want to start your page number at 10. You can then use the @page:first rule to reset the counter for the first page to value 9.

    @page { counter-increment: page }
    @page:first { counter-reset: page 9 }
    

    The combination of both rules will reset the counter for the first page to 9. Then for each page (including the first) it will increment the counter. This results in a counter value of 10 for the first page, 11 for the second and so on.

    You can also use pure css

    Example:

    @page {
        counter-increment: page;
        counter-reset: page 1;
        @top-right {
            content: "Page " counter(page) " of " counter(pages);
        }
    }
    

    ... in theory. In real world only PrinceXML supports this.

    0 讨论(0)
  • 2021-01-12 09:48

    Not using @page, but I have gotten pure CSS page numbers to work in Firefox 20:

    The CSS is:

    #content {
        display: table;
    }
    
    #pageFooter {
        display: table-footer-group;
    }
    
    #pageFooter:after {
        counter-increment: page;
        content: counter(page);
    }
    

    And the HTML code is:

    <div id="content">
      <div id="pageFooter">Page </div>
      multi-page content here...
    </div>
    

    It works on most major browsers

    0 讨论(0)
提交回复
热议问题