Different browsers have weird limitations for page-break-inside: avoid
. Following limitations have been suggested in different articles:
- If the document tree has the parent or grandparent with
display:flex
or display:grid
, avoiding page-breaks will not work.
- If the parent element has
display:inline-block
, avoid
doesn't work.
- In some cases, parent element needs
position:relative
for the value avoid
to work in children. (Exact rules unknown.)
- In some cases, both the parent element and the element that needs to avoid breaking needs
position:relative
for the value avoid
to work. (Exact rules unknown.)
- The parent element MUST NOT have
display: inline-block
.
- The element MUST NOT have
display:table-cell
.
In short, @media print
styles should probably avoid using float
, flex
, grid
, position:absolute
and position:sticky
if you need to use page-break-inside: avoid
. For best compatibility with different browsers, try to define whole tree from root to parent with display:block
and the element that shouldn't break with display:inline-block
in addition to page-break-inside: avoid
.
None of the above exceptions are part of any specs so these are just bugs or limitations in browser implementations.
Also note that even though latest specs prefer break-inside: avoid
instead of page-break-inside: avoid
the real world browser supports is still pretty bad. I would recommend declaring both:
.nobreak
{
page-break-inside: avoid;
break-inside: avoid
}
position: relative
to the parent – Chaschase