Print page numbers on pages when printing html
To smoothly add page numbers to printed HTML documents, take advantage of CSS counters. Apply the counter-reset
property to the body
and increase it with the counter-increment
property on a specific page class. Show the page number using the content
property:
Simply encapsulate your page content with the <div class="page"></div>
tags to activate the numbering:
This code setup provides consecutive page numbers on each printed page, guaranteeing a sophisticated look for your printed works.
Expanding the CSS Counters Method
While the counter-increment
and content
properties provide a working solution, let's consider an ingenious method using the display: table for multi-page content and the :after
pseudo-element for automatically incrementing page numbers.
This neat trick ensures that your page numbers are aligned appropriately and aren't impacted by page content.
Supporting All Browsers
Be aware, different browsers require varied approaches. Specific to Firefox 20+ versions, the solution would be:
This zeroes in 👀 your page numbers with leading zeros and places them center-stage at the bottom.
Accommodating Dynamic Content
For web pages that can change size in real-time, use window.onload
to fire up a function that calculates the pages dynamically.
Using JavaScript and absolute positioned divs
, create dynamic page numbers based on the document scrollHeight
:
Once the document loads, prepare to see the magic ✨ of dynamic page numbers.
Print Features Across Browsers via Paged.js
For a more browser-friendly solution, consider Paged.js. This free polyfill enhances your HTML document's printing features:
Was this article helpful?