balupton
balupton

Reputation: 48650

Page numbers with CSS/HTML

An interesting use case has popped up for us, we are requiring that when we print a website the printed copy will have a page header and footer, and inside the footer the page number.

Anyone got any idea how to achieve this?

Note: Browser version can be the latest of anything, clients are other web developers.

Upvotes: 47

Views: 92910

Answers (2)

raisercostin
raisercostin

Reputation: 9189

Now in 2024 this works with pagedjs that works as a polyfill

https://pagedjs.org/documentation/2-getting-started-with-paged.js/#using-paged.js-as-a-polyfill-in-web-browsers

Sample here

<!DOCTYPE html PUBLIC>
<html lang="en" lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <title>
    pagedjs sample - more complex at raisercostin.org/resume
  </title>
  <script src="https://unpkg.com/[email protected]/dist/paged.polyfill.js"></script>
  <style type="text/css">
    @page {
      size: A7 portrait;
      margin: 100px;
      border: solid 1px blue;

      @top-left {
        content: "top left text";
        border: solid 1px red;
      }

      @bottom-right {
        content: "bottom right text";
        border: solid 1px green;
      }
    }
  </style>
</head>

<body>
  content
</body>

</html>

This should be rendered in chrome as a7 page rendered

Upvotes: 2

alex
alex

Reputation: 490233

Depending on your required browser support.

@page {
  @bottom-right {
    content: counter(page) " of " counter(pages);
  }
}

Further reading:

Upvotes: 41

Related Questions