1

I want to use the value generated by counter(pages) in order to do something specific on the last page of a printed web document using an @page rule.

Using counter(pages) is working fine for me in an at-page context:

    @page {
      @top-right {
        content: counter(pages);
      }
    }

Addressing a specific page also works fine:

    @page: nth(69) {
      @top-right {
        content: normal;
      }
    }

What I want is to pass the total page count to nth(), like so:

    @page: nth(#{counter(pages)}) {
      @top-right {
        content: normal;
      }
    }

Without success.

I've tried using a SASS variable:

    $pagecount: 69; // this works
    $pagecount: '69'; // even this works
    $pagecount: #{counter(pages)}; // but this doesn't
    $pagecount: counter(pages); // nor does this
    ...
    @page: nth(#{$pagecount}) {...}
    ...

When I try to use the dynamic value my code always compiles to

    @page :nth(counter(pages)) {}

which of course does nothing. Is it possible to get the total page count into nth()?

eisnr
  • 11
  • 3
  • .page:last-child { color: red }? In css you do not know anything about html. So you can't get the actual count of elements. – 3rdthemagical May 15 '19 at 09:01
  • Thank you but I think this won't let me do what I want – I want to do something specific to the page margins on the last page using an [@page rule](https://drafts.csswg.org/css-page-3/#at-page-rule). I've edited my question to make this clearer. – eisnr May 15 '19 at 09:15
  • https://stackoverflow.com/a/50041039/4306572 maybe this would be helpful – 3rdthemagical May 15 '19 at 09:34
  • Wow, that's looking very promising although my setup (using Prince for creating a pdf) seems unable to parse named pages. Will try if I can get it to work with weasyprint. Thanks! – eisnr May 15 '19 at 10:13
  • Actually this works great, also with Prince. Thanks again, much appreciated! – eisnr May 15 '19 at 16:25

1 Answers1

0

As pointed out by 3rdthemagical, an answer to this previous question achieves what I want, not by using counter(pages) but instead by using named pages on an element that is positioned on the last page of the document.

eisnr
  • 11
  • 3