title: How to add an auto incremental page number for every printed page?
body: <p>I'm looking for a way to put an auto incremental page number for every printed page, I prefer to achieve that merely using CSS but I appreciate it if you offer any solution using JavaScript.</p>
  
  <p>I utilized <code>counter-reset</code> to create a counter then I used <code>counter-increment</code> as it stated in <a href="https://www.w3schools.com/cssref/pr_gen_counter-increment.asp" rel="nofollow noreferrer">W3Schools</a>, it works fine if there are multiple elements, for instance if we have 10 <code>&lt;li&gt;</code>s but if I have only one <code>&lt;div class="page-footer"&gt;&lt;/div&gt;</code> which is going to be iterated at the bottom of every single page at print, it won't work as it used to work in Firefox.</p>
  
  <p>For more clarification, take a look at following sample and click on <code>Print me</code> button, (you can save it as XPS to simulate printing):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body{
     counter-reset: page;
    }
    .page-header, .page-header-space {
      height: 100px;
    }
  
    .page-footer, .page-footer-space {
      height: 50px;
  
    }
  
    .page-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      border-top: 1px solid black; /* for demo */
      background: yellow; /* for demo */
    }
  
    .page-footer:after {
      counter-increment: page;
      content: counter(page)
    }
  
    .page-header {
      position: fixed;
      top: 0mm;
      width: 100%;
      border-bottom: 1px solid black; /* for demo */
      background: yellow; /* for demo */
    }
  
    .page {
      page-break-after: always;
    }
  
    @page {
      margin: 20mm
    }
  
    @media print {
       thead {display: table-header-group;} 
       tfoot {display: table-footer-group;}
       
       button {display: none;}
       
       body {margin: 0;}
    }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="page-header" style="text-align: center"&gt;
      I'm The Header
      &lt;br/&gt;
      &lt;button type="button" onClick="window.print()" style="background: pink"&gt;
        PRINT ME!
      &lt;/button&gt;
    &lt;/div&gt;
  
    &lt;div class="page-footer"&gt;
      I'm The Footer, Page #
    &lt;/div&gt;
  
    &lt;table&gt;
  
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;!--place holder for the fixed-position header--&gt;
            &lt;div class="page-header-space"&gt;&lt;/div&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;!--*** CONTENT GOES HERE ***--&gt;
            &lt;div class="page"&gt;PAGE 1&lt;/div&gt;
            &lt;div class="page"&gt;PAGE 2&lt;/div&gt;
            &lt;div class="page" style="line-height: 3;"&gt;
              PAGE 3 - Long Content
              &lt;br/&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt metus eu consectetur rutrum. Praesent tempor facilisis dapibus. Aliquam cursus diam ac vehicula pulvinar. Integer lacinia non odio et condimentum. Aenean faucibus cursus
              mi, sed interdum turpis sagittis a. Quisque quis pellentesque mi. Ut erat eros, posuere sed scelerisque ut, pharetra vitae tellus. Suspendisse ligula sapien, laoreet ac hendrerit sit amet, viverra vel mi. Pellentesque faucibus nisl et dolor
              pharetra, vel mattis massa venenatis. Integer congue condimentum nisi, sed tincidunt velit tincidunt non. Nulla sagittis sed lorem pretium aliquam. Praesent consectetur volutpat nibh, quis pulvinar est volutpat id. Cras maximus odio posuere
              suscipit venenatis. Donec rhoncus scelerisque metus, in tempus erat rhoncus sed. Morbi massa sapien, porttitor id urna vel, volutpat blandit velit. Cras sit amet sem eros. Quisque commodo facilisis tristique. Proin pellentesque sodales rutrum.
              Vestibulum purus neque, congue vel dapibus in, venenatis ut felis. Donec et ligula enim. Sed sapien sapien, tincidunt vitae lectus quis, ultricies rhoncus mi. Nunc dapibus nulla tempus nunc interdum, sed facilisis ex pellentesque. Nunc vel
              lorem leo. Cras pharetra sodales metus. Cras lacus ex, consequat at consequat vel, laoreet ac dui. Curabitur aliquam, sapien quis congue feugiat, nisi nisl feugiat diam, sed vehicula velit nulla ac nisl. Aliquam quis nisi euismod massa blandit
              pharetra nec eget nunc. Etiam eros ante, auctor sit amet quam vel, fringilla faucibus leo. Morbi a pulvinar nulla. Praesent sed vulputate nisl. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean commodo
              mollis iaculis. Maecenas consectetur enim vitae mollis venenatis. Ut scelerisque pretium orci id laoreet. In sit amet pharetra diam. Vestibulum in molestie lorem. Nunc gravida, eros non consequat fermentum, ex orci vestibulum orci, non accumsan
              sem velit ac lectus. Vivamus malesuada lacus nec velit dignissim, ac fermentum nulla pretium. Aenean mi nisi, convallis sed tempor in, porttitor eu libero. Praesent et molestie ante. Duis suscipit vitae purus sit amet aliquam. Vestibulum lectus
              justo, lobortis a purus a, dapibus efficitur metus. Suspendisse potenti. Duis dictum ex lorem. Suspendisse nec ligula consectetur magna hendrerit ullamcorper et eget mauris. Etiam vestibulum sodales diam, eget venenatis nunc luctus quis. Ut
              fermentum placerat neque nec elementum. Praesent orci erat, rhoncus vitae est eu, dictum molestie metus. Cras et fermentum elit. Aenean eget augue lacinia, varius ante in, ullamcorper dolor. Cras viverra purus non egestas consectetur. Nulla
              nec dolor ac lectus convallis aliquet sed a metus. Suspendisse eu imperdiet nunc, id pulvinar risus. Maecenas varius sagittis est, vel fermentum risus accumsan at. Vestibulum sollicitudin dui pharetra sapien volutpat, id convallis mi vestibulum.
              Phasellus commodo sit amet lorem quis imperdiet. Proin nec diam sed urna euismod ultricies at sed urna. Quisque ornare, nulla et vehicula ultrices, massa purus vehicula urna, ac sodales lacus leo vitae mi. Sed congue placerat justo at placerat.
              Aenean suscipit fringilla vehicula. Quisque iaculis orci vitae arcu commodo maximus. Maecenas nec nunc rutrum, cursus elit quis, porttitor sapien. Sed ac hendrerit ipsum, lacinia fringilla velit. Donec ultricies feugiat dictum.
            &lt;/div&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
  
      &lt;tfoot&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;!--place holder for the fixed-position footer--&gt;
            &lt;div class="page-footer-space"&gt;&lt;/div&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tfoot&gt;
  
    &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>As you see, there is only one <code>&lt;div class="page-footer"&gt;&lt;/div&gt;</code>.</p>
  
  <p>At print result:</p>
  
  <ol>
  <li><p>in Firefox the total page number will be shown at the first page, for instance if the total pages is 5, the number 5 will be shown at the first <code>page-footer</code> and other <code>page-footer</code> s will be left blank.</p></li>
  <li><p>In Chrome, Opera and Edge, all <code>page-footer</code> s will be set by 1.</p></li>
  </ol>
  
  <p>I even used <code>var(--page)</code> in CSS but nothing changed, is there any solution for that? </p>
  
  <p><strong>UPDATE</strong>: <code>Please don't just tell it's a duplicated question</code>, I almost have read 
  all related questions but none of them works for the scenario I stated, it seems that <code>@page</code> with page numbers doesn't work in browsers anymore.</p>