350

I'm making a HTML report that is going to be printable, and it has "sections" that should start in a new page.

Is there any way to put something in the HTML/CSS that will signal to the browser that it needs to force a page break (start a new page) at that point?

I don't need this to work in every browser out there, I think I can tell people to use a specific set of browsers in order to print this.

sleske
  • 81,358
  • 34
  • 189
  • 227
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243

14 Answers14

568

Add a CSS class called "pagebreak" (or "pb"), like so:

@media print {
    .pagebreak { page-break-before: always; } /* page-break-after works, as well */
}

Then add an empty DIV tag (or any block element that generates a box) where you want the page break.

<div class="pagebreak"> </div>

It won't show up on the page, but will break up the page when printing.

P.S. Perhaps this only applies when using -after (and also what else you might be doing with other <div>s on the page), but I found that I had to augment the CSS class as follows:

@media print {
    .pagebreak {
        clear: both;
        page-break-after: always;
    }
}
JM0
  • 340
  • 1
  • 13
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • 17
    But like all good things in CSS, this doesn't always work consistently across the board, so test the living daylights out of it, lest you have angry users wondering why your site prints piles of extra blank pages! – Zoe Nov 02 '09 at 22:17
  • 19
    According to MDN, `page-break-after` "applies to block elements that generate a box," so using an empty `` element won't work. It's a better idea to apply it to a piece of your content. See https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after – nullability Jan 17 '14 at 17:46
  • 2
    @nullability: Good catch. I had mainly used this at my old job in a WebBrowser control in WinForms which used IE, the gold standard of following standards. – Chris Doggett Jan 17 '14 at 19:25
  • 2
    This did not work for me in Chrome... I'm trying to do a pagebreak inside after a inside a , will it work in this case ?
    – delphirules Nov 05 '18 at 15:25
  • 4
    For some reason, when using this trick in Chrome, 1 row of pixels of next page was leaking into previous one (noticeable when using background-color). I fixed it by using `.pagebreak { min-height: 1px; page-break-before: always; }` – pzmarzly Jan 01 '19 at 17:02
  • 1
    I had same issue. I added
    after every 3rd row and my parent div have display: flex; so I removed it and it was working as I want
    – Dinesh Sharma Jan 17 '20 at 13:58
  • Downvoting because this is a 'legacy property'. See answer below (at the time of this message) – Doug Mead Mar 16 '20 at 18:00
  • See [CSS Page-Break Not Working in all Browsers](https://stackoverflow.com/q/4884380/8068625) for properties that cause issues – Henry Woody May 19 '22 at 18:45
56

Try this link

<style>
@media print
{
h1 {page-break-before:always}
}
</style>
NateFriedman
  • 530
  • 4
  • 9
jfarrell
  • 4,480
  • 3
  • 21
  • 14
  • 10
    If you want to skip the first header on the first page, try `h1:not(:first-child) { page-break-before:always; }` – Jeff Atwood Jul 21 '15 at 09:22
50

First page (scroll down to see the second page)
<div style="break-after:page"></div>
Second page
<br>
<br>
<button onclick="window.print();return false;" />Print (to see the result) </button>

Just add this where you need the page to go to the next one (the text "page 1" will be on page 1 and the text "page 2" will be on the second page).

First page (this will be on page n.1)
<div style="break-after:page"></div>
Second page (This will be on page n.2)
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
  • 2
    `
    ` is what worked for me. The first option didn't work for my particular situation.
    – JustinP Oct 24 '22 at 16:08
  • This is because the previous value `always` (of the deprecated `page-break-*` CSS properties) is now equivalent to the value `page` (in the now recommended properties `break-before` and `break-after`) ; see: https://developer.mozilla.org/en-US/docs/Web/CSS/break-after#page_break_aliases – juanmirocks Mar 09 '23 at 18:56
29

Just wanted to put an update. page-break-after is a legacy property now.

Official page states

This property has been replaced by the break-after property.

Update: Based on recent comments.

Page break aliases

For compatibility reasons, the legacy page-break-after property should be treated by browsers as an alias of break-after. This ensures that sites using page-break-after continue to work as designed. A subset of values should be aliased as follows:

page-break-after    break-after
auto                auto
left                left
right               right
avoid               avoid
always              page

Note: The always value of page-break-* was implemented by browsers as a page break, and not as a column break. Therefore the aliasing is to page, rather than the always value in the Level 4 spec.

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
  • 4
    It should be noted that this property is only supported by Microsoft IE and Edge browsers. https://caniuse.com/#feat=mdn-css_properties_break-after_paged_context_always – Benjamin Nov 22 '19 at 01:20
  • [The link by @Benjamin states today](https://caniuse.com/#feat=mdn-css_properties_break-after_paged_context_always), that `break-after` is supported by latest Microsoft IE `10`-`11`, older Edge `12`-`18` and Firefox (`65`-`113`) browsers. It seems **un**supported by Chrome (`4`-`114`), most Opera except `11.5`, current MS Edge `79`-`111`, IE `6`-`9` and Safari (`3.1`-`16.5`). Can somebody confirm or update these info? That looks like pretty bad support IMHO. – Cadoiz Apr 03 '23 at 10:58
  • 1
    @Cadoiz It's specifically `always` that's unsupported. It should be `break-after: page` instead https://caniuse.com/mdn-css_properties_break-after_paged_context_page – Alexey Romanov Apr 13 '23 at 16:22
15

You can use the CSS property page-break-before (or page-break-after). Just set page-break-before: always on those block-level elements (e.g., heading, div, p, or table elements) that should start on a new line.

For example, to cause a line break before any 2nd level heading and before any element in class newpage (e.g., <div class=newpage>...), you would use

h2, .newpage { page-break-before: always }
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
9

Try this (its work in Chrome, Firefox and IE):

... content in page 1 ...
<p style="page-break-after: always;">&nbsp;</p>
<p style="page-break-before: always;">&nbsp;</p>
... content in page 2 ...
Ousama
  • 2,176
  • 3
  • 21
  • 26
8

For example: below code is present at end of the page and you want to force HTML to print on another page so you can code like this

       <table>
            <tbody>
               <tr>
                  <td width="180" valign="top">
                            <p>
                                <strong> </strong>
                            </p>
                            <p>
                                <strong> </strong>
                            </p>
                            <p>
                                <strong> </strong>
                            </p>
                            <p>
                                <strong> </strong>
                            </p>
                            <p align="center">
                                <strong>Approved by Director</strong>
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>

the main code gose here: desired answer

<div style="page-break-inside:avoid;page-break-after:always"></div>

Now write the remaining code for the second/another page like this

                <table border="1">
                            <tr class="center">
                                <td>
                                    #Main<b>&nbsp;Main</b>
                                </td>
                                <td>
                                    #Project<b>&nbsp;Project</b>
                                </td>
                                <td>
                                    #TypeMainProj<b>&nbsp;Main + Project</b>
                                </td>
                                <td>
                                    #Imprest<b>&nbsp;Imprest</b>
                                </td>
                            </tr>
                        </table>
Ronak Munjapara
  • 459
  • 5
  • 8
5

Below code worked for me and there are some more examples Here

  <div style="page-break-inside:avoid;page-break-after:always">
  </div>
RoshanS
  • 89
  • 2
  • 5
2

Let's say you have a blog with articles like this:

<div class="article"> ... </div>

Just adding this to the CSS worked for me:

@media print {
  .article { page-break-after: always; }
}

(tested and working on Chrome 69 and Firefox 62).

Reference:

Basj
  • 41,386
  • 99
  • 383
  • 673
2

CSS

@media print {
  .pagebreak { 
     page-break-before: always; 
  }
}

HTML

<div class="pagebreak"></div>
2

I was struggling this for some time, it never worked.

In the end, the solution was to put a style element in the head.

The page-break-after can't be in a linked CSS file, it must be in the HTML itself.

4b0
  • 21,981
  • 30
  • 95
  • 142
joe
  • 41
  • 2
1

I needed a page break after every 3rd row while we use print command on browser.

I added

<div style='page-break-before: always;'></div>

after every 3rd row and my parent div have display: flex; so I removed display: flex; and it was working as I want.

mgraph
  • 15,238
  • 4
  • 41
  • 75
Dinesh Sharma
  • 89
  • 1
  • 3
0
  • 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.
  • "page-break-before" also works

Example:

HTML_BLOCK_1
<p style="page-break-after: always"></p>
HTML_BLOCK_2
<p style="page-break-after: always"></p>
HTML_BLOCK_3

While printing the html file with the above code, the print preview will show three pages (one for each html block "HTML_BLOCK_n" ) where as in the browser all the three blocks appear sequentially one after the other.

Chinmay
  • 840
  • 6
  • 9
-7

@Chris Doggett makes perfect sense. Although, I found one funny trick on lvsys.com, and it actually works on firefox and chrome. Just put this comment anywhere you want the page-break to be inserted. You can also replace the <p> tag with any block element.

<p><!-- pagebreak --></p>
Community
  • 1
  • 1
Oniya Daniel
  • 359
  • 6
  • 15