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

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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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

Related Articles

Joes Revolver Map

Joes Word Cloud

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

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 - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.