CSS Page-Break Not Working in all Browsers

2019-01-05 02:54发布

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;
 }
}

8条回答
来,给爷笑一个
2楼-- · 2019-01-05 03:16

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, and block elements with borders.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-05 03:18

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

查看更多
该账号已被封号
4楼-- · 2019-01-05 03:19

what's your code?
like this?:


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

查看更多
5楼-- · 2019-01-05 03:32

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.

查看更多
叼着烟拽天下
6楼-- · 2019-01-05 03:33

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>
查看更多
混吃等死
7楼-- · 2019-01-05 03:37

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;
}

EDIT: I forgot to add that 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.

Hope this helps!

~techdude

查看更多
登录 后发表回答