Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS page x of y for @media print

Tags:

html

css

printing

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 longer works (At least in Firefox 56 [64 bit])

The obsolete method is that there used to be an automatically instantiated CSS counter named pages, so a simple bit of CSS generated from this SASS:

footer {
    position: fixed;
    bottom: 0;
    left: 20px;

    &:after {
        counter-increment: page;
        content: "Page " counter(page) " of " counter(pages);
    }
}

Used to do what I want. Now it displays "Page [x] of 0".

I have tried using this bit of CSS to recreate my own max-page counter:

@page {
    counter-increment: maxpage;
}

However this also returns 0 when used in my footer.

Is there any reasonably cross-browser friendly means of getting this functionality?

like image 250
Scoots Avatar asked Oct 12 '17 13:10

Scoots


People also ask

What is @media print in CSS?

Advertisements. You can use CSS to change the appearance of your web page when it's printed on a paper. You can specify one font for the screen version and another for the print version. You have seen @media rule in previous chapters.

How do I add a print page in CSS?

Developer Tools. The DevTools ( F12 or Cmd/Ctrl + Shift + I ) can emulate print styles, although page breaks won't be shown. In Chrome, open the Developer Tools and select More Tools, then Rendering from the three-dot icon menu at the top right. Change the Emulate CSS Media to print at the bottom of that panel.

How do I put page numbers in CSS?

If you are looking to add page numbers when printing under Chrome/Chromium, one easy solution is to use Paged. js. This JS library takes your HTML/CSS and cuts it into pages, ready to print as a book, that you will preview in your browser. It makes the @page and most the CSS3 specifications work for Chrome.


1 Answers

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.

like image 87
spotnick Avatar answered Oct 21 '22 01:10

spotnick