Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an element to re-appear after a page break?

Tags:

html

css

Background: I have a PHP script that generates a long HTML/CSS document that gets converted to a PDF with WkHtmlToPdf.

At one point in the document, we enter a dynamic area with a variable number of entries, some of which include large images. I want to redraw a header on every page.

Point of clarification: This is a repeated header in the print view of a section of a webpage, not just a header for the entire page.

How I want to do that is via CSS. For example (pseudocode):

    #some_region:pagebreak {
      background-color: #fcc;
      border-color: #000;
      border-style: solid;
      border-width: 0 0 1px 0;
      content: "Our Header Here";
    }

Of course, this fictitious CSS3 selector does not exist!

Are there any clever CSS hacks that can be used to display an element after any page-breaks within a certain container?

To illustrate: https://scott.arciszewski.me/public/23277702.php

like image 563
Scott Arciszewski Avatar asked Apr 24 '14 19:04

Scott Arciszewski


People also ask

How do I make a page-break dynamic in HTML?

page-break-before: auto instead of . page-break-before: always. The "auto" will break the page only if the contents are at the end if the page, this will prevent breaking the page and leaving a lot of blank space. Save this answer.

How do I move the contents to the next page in HTML?

We can add a page break tag with style "page-break-after: always" at the point where we want to introduce the pagebreak in the html page.

How do I stop an element from printing on two pages?

You can use page-break-inside="avoid" on <div> "B". This is probably closer to what you want. If "B" does not fit on the page with "A", "B" will all be moved to the next page.


1 Answers

What about a position: fixed; header in @media print? That would force a header on each printed page. Something like:

    @media screen {
        .print-header {
            display: none;
        }
    }

    @media print {
        .print-header {
            position: fixed;
            ...
        }
    }
like image 145
JimmyRare Avatar answered Nov 08 '22 00:11

JimmyRare