When printing from Chrome, I would like to avoid a page break between a heading and the following paragraph.
I achieved this for most documents with:
h1 {
page-break-inside: avoid;
page-break-after: avoid;
}
... and this moves a bunch of page-breaks, forcing the break to occur before the heading when necessary.
However this seems to fail when the break happens within the margin of the heading.
I can reproduce like this:
<html>
<head>
<style>
h1 {
border-style: solid;
margin: 30px;
page-break-inside: avoid;
page-break-after: avoid;
}
p {
background-color: grey;
}
</style>
</head>
<body>
<h1>Lorem ipsum dolor sit amet</h1>
<p>... consectetur adipiscing elit (etc)...</p>
(Repeat to fill a page)
</html>
... shorten/lengthen the paragraph text until print-preview shows an unwanted page-break:
The CSS spec seems to say this shouldn't happen:
In the normal flow, page breaks can occur at the following places:
1) In the vertical margin between block-level boxes. When an unforced page break occurs here, the used values of the relevant 'margin-top' and 'margin-bottom' properties are set to '0'. When a forced page break occurs here, the used value of the relevant 'margin-bottom' property is set to '0'; the relevant 'margin-top' used value may either be set to '0' or retained.
...
- Rule A: Breaking at (1) is allowed only if the 'page-break-after' and 'page-break-before' properties of all the elements generating boxes that meet at this margin allow it, which is when at least one of them has the value 'always', 'left', or 'right', or when all of them are 'auto'.
Is this working as it should? Or a bug in Chrome's CSS? Or a misunderstanding on my part?
Can I work around it somehow?