View Full Version : A question about CSS: widows and orphans

08-31-2009, 12:41 PM
The CSS and ePUB specifications support these two properties, widows and orphans, which set the minimum number of lines that must be left at the top or bottom of a page, i.e. to avoid leaving single lines at any of these places.

I don't know, however, how these properties work when they are specified for elements that contain other block elements inside, for instance:

<div style="widows=2;orphans=2;">
<p>I kiss the lips of no fair maid,</p>
<p>Nor wet mine with the foaming wine</p>
<p>Within the beaker’s gold.</p>
<p>A cheek upon whose rose-leaf shade</p>
<p>Mine eyes have lit a glow divine,</p>
<p>A look which shyly seeketh mine,</p>
<p>These leave me still and cold.</p>

The intent here is preventing a pagebreak that would cause a single line (the minimum is 2) appearing at the top or bottom of a page. But would this work or would a pagebreak be allowed anyway if it occurs after after a block element, <p>...</p>?

In principle, "page-break-inside:avoid" would prevent any page break inside the stanza, but I don't want to be so restrictive...

Peter Sorotokin
08-31-2009, 03:13 PM
widow/orphan control won't work in that case.

Peter Sorotokin
08-31-2009, 03:15 PM
what you can do is to put page-break-after: avoid on the first p and page-break-before: avoid on the last one.

08-31-2009, 03:30 PM
widow/orphan control won't work in that case.

That's what I feared.

Reading again the relevant part of the spec, tells me that's indeed the case:

In the normal flow, page breaks can occur at the following places:

1. In the vertical margin between block boxes. [...]
2. Between line boxes inside a block box.

These breaks are subject to the following rules:

Rule A: [...]
Rule B: [...]
Rule C: Breaking at (2) is allowed only if the number of line boxes between the break and the start of the enclosing block box is the value of 'orphans' or more, and the number of line boxes between the break and the end of the box is the value of 'widows' or more.
Rule D: [...]

It does not say 'widows' and 'orphans' affect breaks at (1), which is what I'd wish in this case.

08-31-2009, 04:38 PM
An ugly hack would be to make a non-breaking div of the first two lines, then one which was allowed to break of all but the last two lines, then a final non-breaking div of the last two lines.


08-31-2009, 04:54 PM
I'm reluctant to use those "solutions", but thanks, anyway. I think I prefer either leave it alone or use "page-break-inside:avoid". It could be better if the ePUB spec supported the :first-child selector and if there were a :last-child one ;)