I am using Windows 10 with Chrome 79.0.3945.130. I have HTML documents that contain <h3>
tags. When printing, I would like to avoid page breaks immediately after these tags.
The natural way to prevent page breaks seems to be to use the CSS break–after
property. And I find that it works in Microsoft Edge. But I can't get it to work in Chrome or in the current, Chromium-based version of Edge. Here is a minimal example:
<html>
<head><title>Getting CSS page breaks to work</title></head>
<body>
<style>
h3 { break-after: avoid; }
.tall {
height: 9.5in;
background-color: blue;
}
</style>
<div class="tall"></div>
<h3>Section heading</h3>
<p>Lorem ipsum dolor.</p>
</body>
</html>
When I print that document in an old version of Edge (~v44), it prints as it should. But when I print it in Chrome or the current version of Edgs (v83), the <h3>
header appears at the bottom of the page, and the following <p>
text appears at the top of the following page. Breaking the page between <h3>
and <p>
is what I want to prevent.
I can get around the problem by wrapping both the <h3>
and the <p>
tag in a div and setting break–inside: avoid
for that div. But this solution is unsatisfactory: it is ad hoc, requiring me to first identify all of the cases in which inappropriate breaks appear.
I have tried many variations on the example given here. For example, I've tried setting the break-* properties in conjunction with the ::after
selector, as in h3::after { break-after: avoid; }
. But I haven't succeeded in preventing page breaks.
There are also some related posts, for example:
- CSS Printing: Avoiding cut-in-half DIVs between pages?
- page-break/webkit-region-break not working in chrome anymore?)
but the advice given there doesn't help with the example given above.
What else can I do?
` element `float:left` might help with the goal?
– Paul T. Feb 08 '20 at 16:50