CSS Printing - Page Break Inside with Headers and Footers

What?
I saw a lot of articles that would cover this but I wanted an example which includes headers and footers and how to get an automatic page-break-inside to not overlap these. Fine on screen but this is obviously for when it comes to printing.

Why?
I needed to create a template in HTML where the first page is a cover page (background image filling the page with a logo floating at the centre) and the next page has a table which is of variable length. Fine when the table was short and didn't have many rows. But the client will pick up the phone to you when the table has too many rows to fit on one page, and this overlaps onto the next, covering the footer and header...

How?
By spending a lot of time spinning on my chair and twiddling my thumbs...

Firstly, set the page CSS: This will allow 3cm from the top for a header and 2cm from the bottom for a footer.
copyraw
@page
        {
            size: A4;
            margin: 3cm 1cm 2cm 1cm;
        }
        @page :first {
            size: A4;
            margin: 0cm 0cm 0cm 0cm;
        }
  1.  @page 
  2.          { 
  3.              size: A4; 
  4.              margin: 3cm 1cm 2cm 1cm
  5.          } 
  6.          @page :first { 
  7.              size: A4; 
  8.              margin: 0cm 0cm 0cm 0cm
  9.          } 
If this works for you then brilliant because it didn't work for me... I guess I'm special, this is what I had to do:
copyraw
@page
        {
            size: A4;
            margin: 0cm 0cm 0cm 0cm;
        }
        .page {
            page-break-after: always;
            width:21cm;
            height:29.7cm;
            margin:0mm 0mm 0mm 0mm;
            padding:3cm 1cm 2cm 1cm;
            display: table;
        }
  1.  @page 
  2.          { 
  3.              size: A4; 
  4.              margin: 0cm 0cm 0cm 0cm
  5.          } 
  6.          .page { 
  7.              page-break-after: always; 
  8.              width:21cm
  9.              height:29.7cm
  10.              margin:0mm 0mm 0mm 0mm
  11.              padding:3cm 1cm 2cm 1cm
  12.              display: table; 
  13.          } 
Category: Cascading Stylesheets :: Article: 670

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Please publish modules in offcanvas position.