Finding your way through a long document / Page numbered footer
Page 3 of 12 in a footer, on every page but the cover
The folio, printed in a page margin box, usually as a number and a total, and usually required to be absent from the first page.
Why this block is harder than it looks
The number itself is easy. What is not easy is everything conditional around it: no folio on the cover, a different position on left and right pages for a document that will be bound, a total that has to count the pages that exist rather than the pages you expected, and a starting number that may not be one. All of that is done in page rules rather than in the content, and the reason people struggle with it is that they look for it in the content, where none of the levers are.
The decisions that make it work
Reasons rather than a description of the code. Each of these is a choice that has a wrong answer, and the wrong answer is usually the default.
- Put the folio in a page margin box using counter(page), and the total with counter(pages). Both are provided by the pagination step and neither exists in the body.
- Suppress it on the cover with a named page whose margin boxes are empty, not with a selector, because there is no selector that reaches a margin box.
- For a bound document, mirror the position using the left and right page pseudo-classes, so the number is always on the outer edge.
- Set it in tabular figures. Once the document passes ten pages, a proportional 1 makes the folio jump position between pages.
- Include the total only if the total is meaningful. Page 3 of 12 tells a reader whether they have the whole document, which matters for anything posted or faxed.
- Reset the counter deliberately if the numbering should start after front matter, and check the resulting last number against the real page count.
The fragment
Markup and the CSS it needs, and nothing else. It expects a document that already has a page rule and a base stylesheet, so paste it into one rather than opening it on its own.
<style>
@page {
size: A4;
margin: 22mm 20mm 26mm;
}
/* outer edge on both sides, for a document that will be bound */
@page :left { @bottom-left { content: counter(page) " of " counter(pages);
font-size: 8pt; color: #666;
font-variant-numeric: tabular-nums; } }
@page :right { @bottom-right { content: counter(page) " of " counter(pages);
font-size: 8pt; color: #666;
font-variant-numeric: tabular-nums; } }
@page cover {
margin: 30mm 25mm;
@bottom-left { content: ""; }
@bottom-right { content: ""; }
}
.cover { page: cover; break-after: page; }
</style>What breaks when it is built the obvious way
Trying to hide the folio on the first page with a rule about the first element in the body. Margin box content is not part of the document tree, so nothing in the body has any relationship to it. The only mechanism is a separate page rule.
How to prove it survived pagination
Render a document long enough to reach double digits and confirm the folio does not shift horizontally between page 9 and page 10. Then confirm the last page's number equals the real page count, and that the cover has none.
Where it sits in a finished document
This page is one block. The document it belongs to has a page rule, a base stylesheet, a header and everything else around it, and repeating all of that here would make thirty pages that say the same thing. The multi-page report template is a complete file with this block already in it, so take that and change the fragment rather than assembling one from parts. The property doing most of the work here is covered on its own page, with the support caveats that belong there rather than here.
Frequently asked
Will this fragment work on its own?
Not as a whole document. It has no page rule, no margins and no base type, because those belong to the document rather than to the block, and duplicating them in every fragment would mean thirty copies to keep in step. Paste it into a template that already has them.
Why does it look right in a browser and wrong in the PDF?
Because a browser window is one continuous surface and a document is a stack of fixed rectangles. Nothing in a scrolling view exercises a page boundary, so every break rule in the fragment is inert until the content is paginated. Render the real thing with enough content to cross two or three boundaries, then look.
Do I need a special option on the render request for this?
No. Everything on this page is CSS and markup, which is your side of the boundary. What the render has to give you is a real page size with real margins, and after that the layout is decided by the stylesheet.
Other parts of a document
The blocks that sit next to this one, and one from the next group along so you are not sealed inside a single kind of problem.
A running header that shows the current section on every page
A line at the top of each page carrying the document title and, ideally, the section the reader is currently inside.
An appendix with its own numbering that starts on a fresh page
Supporting material at the end of a document, numbered separately from the body so it can be added to without renumbering anything.
Marking a table or section that continues on the next page
The word continued, printed at the foot of a split block or in the repeated header of a table, so a reader knows the page is not the whole thing.
A border or frame around every page of a certificate or diploma
A decorative rule or ornamental frame drawn around the whole page, used on certificates, awards and formal documents.
Multi-page report, as a complete file
The whole document with this block already in place, ready to copy and change.
@top-center and the margin boxes in paged output
The property carrying the weight here, with its real support status.
Every part of a document
The full list, grouped by the kind of problem the block poses.
Paste the fragment into the playground inside a page rule and see what it does at a real page size. That is the only way any of this gets confirmed.