Skip to content

Pagination and repeating headers

Start with the page dimensions your users need to print. Set page margins before laying out the body. Test with enough records to reach a second page.

Set size, margins, and page numbers

@page {
  size: A4;
  margin: 20mm 16mm;
  @bottom-right { content: "Page " counter(page) " of " counter(pages); }
}
body { margin: 0; }

Use a margin box for text shared by every page. Leave enough page margin for the header or footer before increasing its font size.

Repeat a running header

Give an element a name and place it in the top margin. The name in running() and element() must match.

<style>
  @page {
    margin: 25mm 16mm 18mm;
    @top-left { content: element(report-heading); }
  }
  .running-header { position: running(report-heading); }
</style>
<div class="running-header">Operations report</div>
<h1>Monthly summary</h1>

Use thead for headings that should repeat as a table continues. The paginated statement shows both the CSS and the final markup.

Control breaks without fixing the page height

Use break-before: page to start a new section, break-inside: avoid for a short block that should stay together, and break-after: avoid to keep a heading with its following content. A block that is taller than the usable page still needs to flow. For native layouts, see the equivalent document properties.

Inspect page counts and the first row on each page with both typical and maximum-sized inputs. Use layout diagnostics for overflow and nearly empty final pages.