热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CSS打印:避免在页面之间切成两半?-CSSPrinting:Avoidingcut-in-halfDIVsbetweenpages?

Imwritingaplug-inforapieceofsoftwarethattakesabigcollectionofitemsandpopsthemint

I'm writing a plug-in for a piece of software that takes a big collection of items and pops them into HTML in a WebView in Cocoa (which uses WebKit as its renderer, so basically you can assume this HTML file is being opened in Safari).

我正在为一款软件编写一个插件,该软件收集大量项目,并将它们在Cocoa的WebView中弹出到HTML中(它使用WebKit作为渲染器,因此基本上您可以假设这个HTML文件正在Safari中打开)。

The DIVs it makes are of dynamic height, but they don't vary too much. They're usually around 200px. Anyway, with around six-hundred of these items per document, I'm having a really rough time getting it to print. Unless I get lucky, there's an entry chopped in half at the bottom and top of every page, and that makes actually using printouts very difficult.

它制作的DIVs是动态高度,但他们没有太多变化。他们通常在200 px。不管怎么说,每一份文档中大约有600个这样的条目,我很难把它打印出来。除非我很幸运,否则每一页的底部和顶部都有一个条目被切成两半,这使得使用打印输出变得非常困难。

I've tried page-break-before, page-break-after, page-break-inside, and combinations of the three to no avail. I think it might be WebKit not properly rendering the instructions, or maybe it's my lack of understanding of how to use them. At any rate, I need help. How can I prevent the cutting-in-half of my DIVs when printing?

我已经尝试过了page-break-before, page-break-after, page-break-inside,以及三者的组合都没有效果。我认为可能是WebKit没有正确地呈现指令,或者可能是我不理解如何使用它们。无论如何,我需要帮助。在印刷时如何防止插页?

8 个解决方案

#1


245  

This should work:

这应该工作:

@media print  
{
    div{
        page-break-inside: avoid;
    }
}

Please note current browser support (12-03-2014):

请注意当前浏览器支持(12-03-2014):

  • Chrome - 1.0+
  • 铬- 1.0 +
  • Firefox (Gecko) - 19.0+
  • Firefox(壁虎)- 19.0 +
  • Internet Explorer - 8.0+
  • Internet Explorer - 8.0 +
  • Opera - 7.0+
  • 歌剧- 7.0 +
  • Safari - 1.3+ (312)
  • Safari 1.3 +(312)

#2


17  

Only a partial solution: The only way I could get this to work for IE was to wrap each div in it's own table and set the page-break-inside on the table to avoid.

只有部分解决方案:我能让IE工作的唯一方法是将每个div包装在它自己的表中,并在表中设置页-break-inside,以避免。

#3


3  

I have the same problem bu no solution yet. page-break-inside does not work on browsers but Opera. An alternative might be to use page-break-after: avoid; on all child elements of the div to keep togehter ... but in my tests, the avoid-Attribute does not work eg. in Firefox ...

我有同样的问题,但还没有解决方案。打开页面-在浏览器上不工作,而是在Opera上工作。另一种选择可能是使用page-break-after: avoid;在所有子元素的div保持在一起…但是在我的测试中,回避属性不起作用。在Firefox中……

What works in all ppular browsers are forced page breaks using eg. page-break-after: always

在所有的ppular浏览器中使用的是强制分页符。page-break-after:总是

#4


2  

The possible values for page-break-after are: auto, always, avoid, left, right

页面中断后的可能值是:auto, always, avoid, left, right

I believe that you can’t use thie page-break-after property on absolutely positioned elements.

我相信你不能在绝对定位的元素上使用thie page-break-after属性。

#5


2  

page-break-inside: avoid; definitely does not work in webkit, in fact has been a known issue for 5+ years now https://bugs.webkit.org/show_bug.cgi?id=5097

page-break-inside:避免;肯定不能在webkit中工作,事实上已经有5年了,现在https://bugs.webkit.org/show_bug.cgi?id=5097

As far as my research has gone, there is no known method to accomplish this (I am working on figuring out my own hack)

就我的研究而言,还没有已知的方法来实现这个目标(我正在研究我自己的方法)

The advice I can give you is, to accomplish this functionality in FF, wrap the content that you don;t want to break ever inside a DIV (or any container) with overflow: auto (just be careful not to cause weird scroll bars to show up by sizing the container too small).

我可以给您的建议是,要在FF中实现这个功能,请将不希望在DIV(或任何容器)中被溢出的内容打包:auto(只要注意不要因为容器的大小太小而导致奇怪的滚动条出现)。

Sadly, FF is the only browser I managed to accomplish this in, and webkit is the one I am more worried about.

遗憾的是,FF是我成功完成的唯一浏览器,而webkit是我最担心的。

#6


2  

page-break-inside: avoid; gave me trouble using wkhtmltopdf.

page-break-inside:避免;我在使用wkhtmltopdf时遇到了麻烦。

To avoid breaks in the text add display: table; to the CSS of the text-containing div.

为避免文本中出现中断,添加显示:table;到包含文本的div的CSS。

I hope this works for you too. Thanks JohnS.

我希望这对你也有用。谢谢你约翰。

#7


1  

One pitfall I ran into was a parent element having the 'overflow' attribute set to 'auto'. This negates child div elements with the page-break-inside attribute in the print version. Otherwise, page-break-inside: avoid works fine on Chrome for me.

我遇到的一个陷阱是一个父元素,它的'overflow'属性设置为'auto'。这将使用打印版本中的page-break-inside属性来否定子div元素。否则,页面-内部-内部:避免在Chrome上为我工作。

#8


0  

In my case I managed to fix the page break difficulties in webkit by setting my selected divs to page-break-inside:avoid, and also setting all elements to display:inline. So like this:

在我的例子中,我通过将所选的div设置为page-break-inside:avoid以及设置所有要显示的元素:inline来解决webkit中的页面中断问题。所以是这样的:

@media print{
* {
    display:inline;
}
script, style { 
    display:none; 
}
div {
    page-break-inside:avoid;
}

}

It seems like page-break-properties can only be applied to inline elements (in webkit). I tried to only apply display:inline to the particular elements I needed, but this didn't work. The only thing that worked was applying inline to all elements. I guess it's one of the large container div' that's messing things up.

看起来页面-break-properties只能应用于内联元素(在webkit中)。我尝试只对我需要的特定元素应用display:inline,但这并不奏效。唯一有效的方法是对所有元素应用内联。我猜是一个大容器div把事情搞砸了。

Maybe someone could expand on this.

也许有人可以对此进行扩展。


推荐阅读
  • 本文整理了常用的CSS属性及用法,包括背景属性、边框属性、尺寸属性、可伸缩框属性、字体属性和文本属性等,方便开发者查阅和使用。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 本文介绍了网页播放视频的三种实现方式,分别是使用html5的video标签、使用flash来播放以及使用object标签。其中,推荐使用html5的video标签来简单播放视频,但有些老的浏览器不支持html5。另外,还可以使用flash来播放视频,需要使用object标签。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 由于同源策略的限制,满足同源的脚本才可以获取资源。虽然这样有助于保障网络安全,但另一方面也限制了资源的使用。那么如何实现跨域呢,以下是实现跨域的一些方法。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了css回到顶部按钮相关的知识,希望对你有一定的参考价值。 ... [详细]
  • GO语言 包 if..else.. for循环 switch 数组
    包1.什么是包1.新建一个文件夹,内部写很多go文件,但是包名必须一致,改文件夹就是一个包2.作用和优点包用于组织Go源代码,提供了更好的可重用性与可读性。由于包提供了代码的封装, ... [详细]
  • 语法必须遵守的语法推荐遵守语法不做要求文件格式文件应该使用Unicode(UTF-8)编码保存。同时不要使用字节序标记(BOM)。与UTF-16和 ... [详细]
  • 为什么三角形与菜单背景的颜色不同? - Why is the triangle a different colour shade to the menu background?
    Imnotunderstandingastowhythetrianglewhichappearswhenthemousehoversoverthemenuitem, ... [详细]
  • Scrapy 爬取图片
    1.创建Scrapy项目scrapystartprojectCrawlMeiziTuscrapygenspiderMeiziTuSpiderhttps:movie.douban.c ... [详细]
  • 详解 Python 的二元算术运算,为什么说减法只是语法糖?[Python常见问题]
    原题|UnravellingbinaryarithmeticoperationsinPython作者|BrettCannon译者|豌豆花下猫(“Python猫 ... [详细]
  • 移动传感器扫描覆盖摘要:关于传感器网络中的地址覆盖问题,已经做过很多尝试。他们通常归为两类,全覆盖和栅栏覆盖,统称为静态覆盖 ... [详细]
  • css div中文字位置_超赞的 CSS 阴影技巧与细节
    本文的题目是CSS阴影技巧与细节。CSS阴影,却不一定是box-shadow与filter:drop-shadow,为啥?因为使用其他属性 ... [详细]
  • 在给定置信度下,判断检测到给定值时所需要的样本量;也能计算在某样本量内能检测到给定效应值的概率功效是1-二类错误,1-β,看做真实效应发生的概率效应值是在备选或研究假设下效应的量对 ... [详细]
author-avatar
dsjdsjdsjjk_896
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有