Google Chrome Printing Page Breaks
Asked Answered
C

15

120

I'm trying to get google chrome to do page breaks.

I've been told via a bunch of websites that page-break-after: always; is valid in chrome but I can not seem to get it to work even with a very simple example. is there any way to force a page break when printing in chrome?

Childe answered 27/10, 2009 at 13:32 Comment(1)
It appears that this has relatively recently (February 2014) been discussed (on an old 2005 bug ticket) on the webkit bug tracker bugs.webkit.org/show_bug.cgi?id=5097Cassirer
B
151

I've used the following approach successfully in all major browsers including Chrome:

<!DOCTYPE html>

<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Paginated HTML</title>
    <style type="text/css" media="print">
      div.page
      {
        page-break-after: always;
        page-break-inside: avoid;
      }
    </style>
  </head>
  <body>
    <div class="page">
      <h1>This is Page 1</h1>
    </div>
    <div class="page">
      <h1>This is Page 2</h1>
    </div>
    <div class="page">
      <h1>This is Page 3</h1>
    </div>
  </body>
</html>

This is a simplified example. In the real code, each page div contains many more elements.

Bebe answered 29/10, 2009 at 22:55 Comment(10)
Ah i see my problem i think. I was attempting to use it with a <br/> tagChilde
@Mike Thanks for answering the problem for me. Not sure why a br doesn't work and a div does, but nonetheless an easy change.Implicative
this works when you try to use with parent page, but does not work when you print from iframe (in chrome, of course).Scholl
Also read @peter's answer below; he also had a good point about position: relative.Christoper
According to spec break-after and break-before apply only to block-level elements, table row groups, table rows (but see prose): drafts.csswg.org/css-break-3/#break-between – that means no floats or any fancy positioning tricks.Graviton
It do not work if the container uses display:flexDib
This worked for me ONLY if the div.page CSS was in an inline style tag with media set to "print". In other words it does not work inside a @media print element in my global style sheet.Nikkinikkie
@Dib thanks! This solved my problem. So, in my @media print {} I set the section to display:block instead of flex. Took me quite some time to find your hint. !Azarria
That display: flex container bug in Chrome should be similar to this Mozilla (Firefox) one: bugzilla.mozilla.org/show_bug.cgi?id=1622935Chicalote
Anyone know why the page-break-inside: avoid; line is necessary? I naïvely thought this should work with that line, but I am wrong, and don't understand why.Noli
I
38

Actually one detail is missing from the answer that is selected as accepted (from Phil Ross)....

it DOES work in Chrome, and the solution is really silly!!

Both the parent and the element onto which you want to control page-breaking must be declared as:

position: relative

check out this fiddle: http://jsfiddle.net/petersphilo/QCvA5/5/show/

This is true for:

page-break-before
page-break-after
page-break-inside

However, controlling page-break-inside in Safari does not work (in 5.1.7, at least)

i hope this helps!!!

PS: The question below brought up that fact that recent versions of Chrome no longer respect this, even with the position: relative; trick. However, they do seem to respect:

-webkit-region-break-inside: avoid;

see this fiddle: http://jsfiddle.net/petersphilo/QCvA5/23/show

so i guess we have to add that now...

Hope this helps!

Iddo answered 12/9, 2012 at 10:58 Comment(6)
It depends on the version of WebKit used if it works or not in Chrome. You yourself said it does not work in Safari 5.1.7, so I don't think it's silly at all. The version of WK in what they tested might be different from what you tested.Groundage
That page doesn't work in Chrome 26.0.1410.65, I get 3 pages and the last one only has one word on it). I also tried making sure both the element I want to apply the rule to and its parent have position:relative on my own page, but it still won't add the page break when printing. So is this a feature that comes and goes in Chrome?Elvera
You're right.... it doesn't seem to work in recent versions of chrome; however, you can replace it with: -webkit-region-break-inside: avoid; (see this one: jsfiddle.net/QCvA5/22/show)Iddo
@Iddo Any updates here? I posted an SO question with bounty trying to fix a situation I have using page-break-inside. Any help?Subaqueous
This worked for me with one oddity - it only works if I put the style inline, and not in the seperate css file. I'm using it on an h7 element. Chrome version is 38.0.2125.111 m. <h7 class="page-breaker" style="display: block;position: relative;page-break-before:always">Grade <?php echo $grade;?></h7>Matthew
My Bad. Works in css file as well. Had media set to screen when calling the css file.Matthew
C
16

I just wanted to note here that Chrome also ignores page-break-* css settings in divs that have been floated.

I suspect there is a sound justification for this somewhere in the css spec, but I figured noting it might help someone someday ;-)

Just another note: IE7 can't acknowledge page break settings without an explicit height on the previous block element:

http://social.msdn.microsoft.com/forums/en-US/iewebdevelopment/thread/fe523ec6-2f01-41df-a31d-9ba93f21787b/

Caladium answered 14/7, 2011 at 22:23 Comment(2)
Likely because floating an element takes it out of the document flow in the same way absolutely positioning it does. Elements with position:absolute won't work with page-break-*.Elvera
Also: there shouldn't be any floating or absolute positioned elements among all parents of the element with page break style. I had bootstrap "col-xs-12" parent, when I explicitly set "float: none" to it, the page break began to work!Crier
A
13

I had an issue similar to this but I found the solution eventually. I had overflow-x: hidden; applied to the <html> tag so no matter what I did below in the DOM, it would never allow page breaks. By reverting to overflow-x: visible; it worked fine.

Hopefully this helps somebody out there.

Apelles answered 12/3, 2012 at 16:17 Comment(1)
Note that any parent element with certain overflow properties can cause this issues. I just added the rule * { overflow-x: visible }.Behemoth
F
10

I'm having this problem myself - my page breaks work in every browser but Chrome - and was able to isolate it down to the page-break-after element being inside a table cell. (Old, inherited templates in the CMS.)

Apparently Chrome doesn't honor the page-break-before or page-break-after properties inside table cells, so this modified version of Phil's example puts the second and third headline on the same page:

<!DOCTYPE html>

<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Paginated HTML</title>
    <style type="text/css" media="print">
      div.page
      {
        page-break-after: always;
        page-break-inside: avoid;
      }
    </style>
  </head>
  <body>
    <div class="page">
      <h1>This is Page 1</h1>
    </div>

    <table>
    <tr>
        <td>
            <div class="page">
              <h1>This is Page 2</h1>
            </div>
            <div class="page">
              <h1>This is, sadly, still Page 2</h1>
            </div>
        </td>
    </tr>
    </table>
  </body>
</html>

Chrome's implementation is (dubiously) allowed given the CSS specification - you can see more here: http://www.google.com/support/forum/p/Chrome/thread?tid=32f9d9629d6f6789&hl=en

Foretoken answered 14/5, 2010 at 9:1 Comment(1)
This was my problem - forced into using table cells for layout by SharePoint 2007, so Chrome was obeying any print style sheet layout declarations :(Tawnyatawsha
L
10

Beware of CSS : display:inline-block when printing.

None of the CCS property to go to next page would work for me in Chrome and Firefox if my table was inside a div with the style display:inline-block

For example, the following doesn't work :

<div style='display:inline-block'>
  <table style='page-break-before:always'>
    ...
  </table>
  <table style='page-break-before:always'>
    ...
  </table>
</div>

But the following work :

<div>
  <table style='page-break-before:always'>
    ...
  </table>
  <table style='page-break-before:always'>
    ...
  </table>
</div>
Lulu answered 3/6, 2014 at 13:48 Comment(2)
And "float" as well. There shouldn't be any "float" properties in parent elements.Ardenardency
Same with display: flexSubmit
B
4

2016 update:

Well, I got this problem, when I had

overflow:hidden

on my div.

After I made

@media print {
   div {
      overflow:initial !important
   }
}

everything became just fine and perfect

Bahuvrihi answered 27/10, 2009 at 13:32 Comment(2)
This solved the problem when nothing else did. Thanks!Animosity
That's is a correct option to remember! The content may be just hidden.Phyllys
U
4

I faced this issue on chrome before and the cause for it is that there was a div has min-height set to a value. The solution was to reset min-height while printing as follows:

@media print {
    .wizard-content{
        min-height: 0;
    }
}
Unionize answered 6/9, 2015 at 15:43 Comment(0)
G
3

This did the trick for me (2021 Chrome):

@media print {
  .page-break {
    display: block;               // <== this can be missing sometimes
    break-before: always;
    page-break-before: always;
  }
}
Goosestep answered 18/5, 2021 at 15:57 Comment(1)
Thank you ! What I was missing was the display: block;Kristinkristina
W
1

If you are using Chrome with Bootstrap Css the classes that control the grid layout eg col-xs-12 etc use "float: left" which, as others have pointed out, wrecks the page breaks. Remove these from your page for printing. It worked for me. (On Chrome version = 49.0.2623.87)

Wareroom answered 29/8, 2016 at 7:28 Comment(0)
J
1

It's now 2021 and this topic is the first result when searching for the exact issue with Chrome. I found this is a very simple solution that works and can be slapped into your without any additional effort to at least affect Chrome page breaking in the middle of a :

<style>
@media print {
    tr, th, td {
        page-break-inside: avoid !important;
    }
}
</style>

Hopefully that helps save someone time.

Jesselton answered 12/5, 2021 at 19:29 Comment(0)
D
0

Have that issue. So long time pass... Without side-fields of page it's break normal, but when fields appears, page and "page break space" will scale. So, with a normal field, within a document, it was shown incorrect. I fix it with set

    width:100%

and use

div.page
  {
    page-break-before: always;
    page-break-inside: avoid;
  }

Use it on first line.

Dalliance answered 15/5, 2014 at 14:11 Comment(0)
B
0

As far as I know the only way to get the correct page breaks in tables with Google Chrome is giving it to the element <tr> the property display: inline-table (or display: inline-block but it fits better in other cases that are not tables). Also should be used the properties "page-break-after: always; page-break-inside: avoid;" as written by @Phil Ross

<table>
  <tr style="display:inline-table;page-break-after: always; page-break-inside: avoid;">
    <td></td>
    <td></td>
    ...
  </tr>
</table>
Bucko answered 5/1, 2016 at 18:46 Comment(2)
Any hints on getting this to not break the margin/padding inside the tds?Dusk
@Dusk you should use the <tr> with "padding: 0; margin: 0;" and give the margin properties to the elements contained within.Bucko
F
0

I was printing 16 labels on A4 page landscape rotation, 4 labels per row, page was breaking in the last row and only 12 label were on one page in chrome only, I was using display:inline-block; on a div, then replaced it with float:right; and it worked!

Furfuran answered 2/8, 2022 at 5:14 Comment(0)
Z
-2

It was working for me when I used padding like:

<div style="padding-top :200px;page-break-inside:avoid;">
   <div>My content</div>
</div>
Zielsdorf answered 8/11, 2017 at 1:23 Comment(1)
This only worked because of the padding and not because of the page-breakWedding

© 2022 - 2024 — McMap. All rights reserved.