CSS Page-Break Not Working in all Browsers
Asked Answered
E

9

64

I'm having trouble getting this working in most browsers, except for IE (it even works correctly in IE6) and Opera.

Firefox separates the divs correctly but only prints the first page.

Chrome and Safari only applies the page break to the last div.

How can I get this working across all browsers correctly?

The HTML:

<div id="leftNav">
  <ul>
    <!--links etc-->
  </ul>
</div>
<div id="mainBody">
 <div id="container">
  <div class="pageBreak">
   <!--content-->
  </div>
  <div class="pageBreak">
   <!--content-->
  </div>
  <div class="pageBreak">
   <!--content-->
  </div>
 </div>
</div>

The divs with the IDs #leftNav and #mainBody are are set to float:left, so they display nicely.

I only want to print the .pageBreak classes, hiding the #leftNav and the rest of the #mainBody with CSS.

The CSS:

@media print
{
 #leftNav
 {
  display:none;
 }
 #mainBody
 {
  border:none;
  margin:none;
  padding:none;
 }
}
Erikerika answered 3/2, 2011 at 9:33 Comment(3)
As far as I know that property should work in all major browsers if you use the "always" value. Could you post your relevant css and html?Con
If you've solved the problem yourself and it doesn't happen to be one of the answers below, it would be a good idea to post it here for posterities sake, instead of appending [solved] to your question's title (which doesn't do anything really). Also note that SitePoint reference (which sepehr linked to below) is a much better reference for CSS and HTML than W3SchoolsThirzia
This was one of my first posts and force of habit made me change the title as per many forum rules instead of making a new post. I did visit SitePoint regarding this issue but that didn't give me the help I needed. In this case W3Schools did.Erikerika
E
119

Parent elements can not have float on them.

Setting float:none on all parent elements makes page-break-before:always work correctly.

Other things that can break page-break are:

  • using page-break inside tables
  • floating elements
  • inline-block elements
  • block elements with borders
Erikerika answered 15/3, 2011 at 16:12 Comment(5)
Which browsers have problems with inline-block-elements?Fantast
The last statement of your answer is very important... I had to cut my <table> in many parts and set different fixed size on every single columns so the said columns would not adjust to the content and still look great. I now wish to destroy every single printer in the world... Thanks for the clarificationsUnderthrust
Also display: flex; will break it. At least in my experience in Chrome :)Guildsman
As well make sure your element isn't hidden. I had that problem once when I wanted to place page breaks on elements with specific content in it. So I set them to hidden and added a page break on the same element which did not work.Kernite
For me it, there was a parent container with an inline-block. Updated that to block instead. It works!~ You could test this by using Ctrl + P in ChromeSystematics
T
34

For the sake of completion, and for the benefit of others who are having the same problem, I just want to add that I also had to add overflow: visible to the body tag in order for FireFox to obey the page breaks and even to print more than just the first page.

Transmission answered 22/3, 2013 at 17:45 Comment(7)
For a bit more completeness, display:flex seems to mess things up as well.Fiber
@Rajesh did that affect Chrome too for you? Or just Firefox?Finkle
In Firefox print display:flex messed up, put display:block in print.cssBarby
Sometimes, IE rather requires an overflow: hiddento make break-inside: avoid work correctly.Gettysburg
@media print{ body{ overflow:visible; } } fixed my problem, thank you!Barred
@Fiber it works like a charm, in my case, I print apart of HTML.Maiga
Adding overflow:visible to my body el helped in Edge as well.Columbite
S
24

I've found that Twitter Bootstrap classes add a bunch of stuff to the page which has made it difficult to get page-breaks working. Firefox worked right away, but I've had to follow various suggestions to get it to work in Chrome and, finally, IE (11).

I followed the suggestions here and elsewhere. The only property I "discovered" that I haven't seen yet mentioned is "box-sizing". Bootstrap can set this property to "box-sizing: border-box", which broke IE. An IE-friendly setting is "box-sizing: content-box". I was led to this by the caveat about "block elements with borders" made by Richard Parnaby-King https://mcmap.net/q/93382/-css-page-break-not-working-in-all-browsers.

It looks like it's a bit of an arms race to discover the next property that might break page-breaks.

This is the setting that worked for me (Chrome, FF, IE 11). Basically, it tries to override all the problematic settings on all divs on the printed page. Of course, this might also break your formatting, and that would mean that you'll have to find another way to set up the page.

@media print {

    div { float: none !important; position: static !important; display: inline; 
          box-sizing: content-box !important;
    }

}
Shaman answered 10/2, 2015 at 21:0 Comment(1)
thank you i add display: inline !important; and margin : 0 !important tooMuttonhead
N
11

There is a solution if the parent has float . For the element to which you applied the page-break, make the element overflow:hidden. Thats all. It worked for me.

<div style='float:left'>

<p style='overflow:hidden;page-break-before:always;'></p>

</div>
Now answered 28/4, 2016 at 18:52 Comment(1)
This worked OK, for me using wkhtmltopdf I needed to set html, body { overflow: hidden; } because a template .css was using overflow-x:hidden. This shouldn't be downvoted. Thanks!Euridice
S
9

Although this is not prominently documented, it should be noted that the page-break properties cannot be applied to table elements. If you have any elements that have a display: table; or display:table-cell; applied to them (common in many templates under the clearfix class) then contained elements will ignore the page-break rules. Just cancel out the the rule in your print stylesheet and you should be OK (after the floats have also been removed, of course).

Here is an example of how to do this for the popular clearfix problem.

.clearfix:before, .clearfix:after{  
    display: block!important;
}

The other place I have run into this is when the template declared the entire page (usually called main or main wrapper) with display:inline-block;

If the section is inside of an inline-block, it will not work so keep your eyes open for those as well. Changing or overwriting display:inline-block; with display:block should work.

Shopping answered 21/8, 2013 at 15:46 Comment(0)
G
8

I had a position: absolute; in the div printing that caused this not to work.

Gold answered 17/12, 2013 at 17:0 Comment(0)
B
8

Make sure the parent element has display:block; rather than display: flex;. This helped me fix the issue

Backbreaking answered 31/5, 2020 at 9:22 Comment(0)
M
6

"Firefox versions up to and including 3.5 don’t support the avoid, left, or right values." IE support is also partial you can achieve what needed by :page-break-before:always; which is supported in all browsers "but only print the first page" : I don't think it is css related , I suppose it's sth on print window of browser :)

Mousebird answered 3/2, 2011 at 9:55 Comment(0)
R
5

what's your code?
like this?:


<style>
@media print
{
table {page-break-after:always}
}
@media print
{
table {page-break-before:always}
}
</style>

Redblooded answered 3/2, 2011 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.