问题
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).
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.
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?
回答1:
This should work:
@media print
{
div{
page-break-inside: avoid;
}
}
Please note current browser support (12-03-2014):
- Chrome - 1.0+
- Firefox (Gecko) - 19.0+
- Internet Explorer - 8.0+
- Opera - 7.0+
- Safari - 1.3+ (312)
回答2:
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.
回答3:
page-break-inside: avoid; gave me trouble using wkhtmltopdf.
To avoid breaks in the text add display: table; to the CSS of the text-containing div.
I hope this works for you too. Thanks JohnS.
回答4:
The possible values for page-break-after are: auto, always, avoid, left, right
I believe that you can’t use thie page-break-after property on absolutely positioned elements.
回答5:
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 ...
What works in all ppular browsers are forced page breaks using eg. page-break-after: always
回答6:
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
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).
Sadly, FF is the only browser I managed to accomplish this in, and webkit is the one I am more worried about.
回答7:
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.
回答8:
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:
@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.
Maybe someone could expand on this.
回答9:
@media print{
/* use your css selector */
div{
page-break-inside: avoid;
}
}
For all new browser this solution works. See caniuse.com/#search=page-break-inside
来源:https://stackoverflow.com/questions/907680/css-printing-avoiding-cut-in-half-divs-between-pages