Cross-browser support of `page-break-inside: avoid;`
Webdevout.net is a great place to check browser CSS compatibility.
For page-break-inside
only IE8 and Opera 8+ are shown to support it
avoid page break inside row of table
You might try this with CSS:
<table class="print-friendly">
<!-- The rest of your table here -->
</table>
<style>
table.print-friendly tr td, table.print-friendly tr th {
page-break-inside: avoid;
}
</style>
Most CSS rules don't apply to <tr>
tags directly, because of exactly what you pointed out above - they have a unique display
style, which doesn't allow for these CSS rules. However, the <td>
and <th>
tags within them usually do allow this kind of specification - and you can easily apply such rules to ALL child-<tr>
's and <td>
's using CSS as shown above.
Can I force a page break in HTML printing?
Add a CSS class called "pagebreak" (or "pb"), like so:
@media print {
.pagebreak { page-break-before: always; } /* page-break-after works, as well */
}
Then add an empty DIV tag (or any block element that generates a box) where you want the page break.
<div class="pagebreak"> </div>
It won't show up on the page, but will break up the page when printing.
P.S. Perhaps this only applies when using -after
(and also what else you might be doing with other <div>
s on the page), but I found that I had to augment the CSS class as follows:
@media print {
.pagebreak {
clear: both;
page-break-after: always;
}
}
Safari page-break-inside:avoid not working
Try using display: inline-block;
instead of page-break-inside: avoid;
. You may also want to add vertical-align: top;
and width: 100%;
to make the elements behave like normal block elements instead of inline elements.
This technique has been working reliably since long before page-break-inside: avoid;
was implemented in most browsers. It's still the most reliable cross-platform way to prevent page breaks in a block of content.
If you want to make a table unbreakable, you can set display: inline-table;
on it. Or you can just put it in an inline-block div.
CSS Page-Break Not Working in all Browsers
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
Related Topics
How to Increase the Bullet Size in a Li
CSS Image Layouting Before Image Loaded
Why Selecting by Id Is Not Recommended in CSS
Svg + CSS3 Animation Not Working with Link Markup
Removing Table Lines and Table Space Between Cells in CSS
CSS Use Color from Another Class
Change a Text Input's Value with CSS
How to Assign Custom CSS Class to Arbitrary Arbitrary Rows of H:Datatable
Change the CSS of the Qna Bot Embedded as Webchat
Cssrules/Rules Are Null in Chrome
"Vw" CSS Units in Calc in Chrome Not Working
Grid Layout on <Fieldset>... Bug on Chrome
Media Queries for Tablet Min-Resolution and Max-Resolution
How to Use Srcset and Sizes for Responsive Images
How to Float Text Around Image in React Native
Are CSS Variable Changes Possible Upon a Radio Button's Checked Selector Being Triggered