Edit online

Controlling Page Breaks

If you want to start a new page with each chapter title, or if you want to avoid breaking a table onto two pages or avoid a break after a section title, you can use CSS properties to control the breaks.

CSS offers the following properties to control the page breaking process:
  • page-break-before
  • page-break-after
  • page-break-inside
Edit online

Forcing Page Breaks

To always move the content to the beginning of the page (for example, before the title of a section), you can use:

section h1 {
    page-break-before: always;
}

Or if you only want a specific element to appear on a single page:

div.notice {
    page-break-before: always;
    page-break-after: always;
}
Edit online

Avoiding Page Breaks

Sometimes page breaks should be avoided. For instance, to avoid breaking between a title and the subsequent content:

h1, h2, h3, h4, h5, h6 {
    page-break-after: avoid;
}

Or if you want to avoid breaks inside tables and lists:

table, ol, ul {
    page-break-inside: avoid;
}
Edit online

Page Breaks Between Named Pages

A page break will be created each time there is a change in the page property associated to the elements in a sequence. Suppose that you have a sequence of <div> elements, one of them associated with a "cover" page, and others with a "chapter" page:

div.cover {
    page: cover;
}

div.chapter {
    page: chapter;
}

The XML document:

...
<div class="cover"> Welcome to the User Guide... </div>
<div class="chapter"> Here are the details... </div>
<div class="chapter"> Here are some more details... </div>

In this example, there will be a forced page break between the first <div> (associated to the "cover" page) and the second because of the page change.

The next two <div> elements are not separated by page breaks because they have the same page name ("chapter") and they are grouped in the same page sequence. If you want to style the first page from that sequence in a different way, the selector:
@page content:first{
  background-color: yellow;
}
will apply to the first page from the first "chapter" <div>.
Suppose you want each of the chapters to start a new page sequence, with the first page colored in yellow. To do so, you must declare a new sequence start on the <div> element. This can be done using the extension property -oxy-page-group:
div.chapter {
  page: chapter;
  -oxy-page-group:start;
}
Edit online

Page Breaks Between Lines: Widows and Orphans

There are cases where a page break is placed between paragraph lines. Typically, for aesthetic purposes, the first paragraph should have more than one line. Another constraint might be the number of lines that are moved to the next page and again, it should avoid leaving a single line. CSS defines two properties for this type of control:

  • orphans - This property specifies the minimum number of line boxes that should be left in a paragraph before the page break occurs.
  • widows - This property specifies the minimum number of line boxes of a block container that must be left in a paragraph created on the next page after a break.
The following example shows how to keep the paragraphs at least four lines on the page before the break, and two lines on the page following the break:
p {
  orphans: 4;
  widows: 2;
}
Note: As a difference from the W3C standard, the widows and orphans CSS properties are applied to lists as well (the default is 2). This means that a list that spans consecutive pages will have either zero or at least 2 lines on each of the pages.