简体   繁体   English

CSS 分页符不适用于所有浏览器

[英]CSS Page-Break Not Working in all Browsers

I'm having trouble getting this working in most browsers, except for IE (it even works correctly in IE6) and Opera.除了 IE(它甚至可以在 IE6 中正常工作)和 Opera 之外,我在大多数浏览器中都无法使用它。

Firefox separates the divs correctly but only prints the first page. Firefox 正确分隔 div,但只打印第一页。

Chrome and Safari only applies the page break to the last div. Chrome 和 Safari 仅将分页符应用于最后一个 div。

How can I get this working across all browsers correctly?我怎样才能在所有浏览器上正确地工作?

The HTML: 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. ID 为#leftNav#mainBody的 div 设置为float:left ,因此它们显示得很好。

I only want to print the .pageBreak classes, hiding the #leftNav and the rest of the #mainBody with CSS.我只是想打印.pageBreak类,隐藏#leftNav和其余#mainBody与CSS。

The CSS: CSS:

@media print
{
 #leftNav
 {
  display:none;
 }
 #mainBody
 {
  border:none;
  margin:none;
  padding:none;
 }
}

Parent elements can not have float on them.父元素不能有float

Setting float:none on all parent elements makes page-break-before:always work correctly.在所有父元素上设置float:none会使page-break-before:always正常工作。

Other things that can break page-break are:其他可以打破page-break事情是:

  • using page-break inside tables在表格内使用page-break
  • floating elements浮动元素
  • inline-block elements inline-block元素
  • block elements with borders带边框的块元素

为了完成,也为了其他有同样问题的人的利益,我只想补充一点,我还必须添加overflow: visible对 body 标记overflow: visible ,以便 FireFox 遵守分页符甚至打印不仅仅是第一页。

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.我发现 Twitter Bootstrap 类向页面添加了一堆东西,这使得分页符很难工作。 Firefox worked right away, but I've had to follow various suggestions to get it to work in Chrome and, finally, IE (11). Firefox 马上就可以工作了,但我不得不遵循各种建议才能让它在 Chrome 中工作,最后在 IE (11) 中工作。

I followed the suggestions here and elsewhere.我遵循了这里和其他地方的建议。 The only property I "discovered" that I haven't seen yet mentioned is "box-sizing".我“发现”但尚未提及的唯一属性是“box-sizing”。 Bootstrap can set this property to "box-sizing: border-box", which broke IE. Bootstrap 可以将此属性设置为“box-sizing:border-box”,这破坏了 IE。 An IE-friendly setting is "box-sizing: content-box".一个 IE 友好的设置是“box-sizing: content-box”。 I was led to this by the caveat about "block elements with borders" made by Richard Parnaby-King https://stackoverflow.com/a/5314590/3397752 .我被 Richard Parnaby-King https://stackoverflow.com/a/5314590/3397752关于“带边框的块元素”的警告所引导。

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).这是对我有用的设置(Chrome、FF、IE 11)。 Basically, it tries to override all the problematic settings on all divs on the printed page.基本上,它会尝试覆盖打印页面上所有 div 上的所有有问题的设置。 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;
    }

}

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;如果您有任何具有display: table;元素display: table; or display:table-cell;display:table-cell; applied to them (common in many templates under the clearfix class) then contained elements will ignore the page-break rules.应用于它们(在 clearfix 类下的许多模板中很常见)然后包含的元素将忽略分页规则。 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 问题执行此操作的示例。

.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;我遇到的另一个地方是当模板使用display:inline-block;声明整个页面(通常称为 main 或 main 包装器)时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.如果该部分位于 inline-block 内,它将不起作用,因此也请睁大眼睛。 Changing or overwriting display:inline-block;更改或覆盖display:inline-block; with display:block should work.使用display:block应该可以工作。

I had a position: absolute;我有一个position: absolute; in the div printing that caused this not to work.在导致这不起作用的 div 打印中。

There is a solution if the parent has float .如果父级有 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>

"Firefox versions up to and including 3.5 don't support the avoid, left, or right values." “Firefox 3.5 及以下版本不支持避免、左或右值。” IE support is also partial you can achieve what needed by :page-break-before:always; IE 支持也是部分的,您可以通过 :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 :)所有浏览器都支持它“但只打印第一页”:我认为它与 css 无关,我想它在浏览器的打印窗口上是 :)

what's your code?你的代码是什么?
like this?:像这样?:


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

Make sure the parent element has display:block;确保父元素有display:block; rather than display: flex;而不是display: flex; . . This helped me fix the issue这帮助我解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM