13 Paged media

Contents

13.1 Introduction to paged media

Paged media (e.g., paper, transparencies, pages that are displayed on computer screens, etc.) differ from continuous media in that the content of the document is split into one or more discrete pages. To handle pages, CSS 2.1 describes how page breaks, CSS2 extends the visual formatting model as follows: Themargins are set on page box extends the box model to allow authors to specify the size of a page, its margins, etc. Theboxes, and how page model extendsbreaks are declared.

The visual formatting model to accountuser agent is responsible for page breaks.transferring the CSS2page model specifies howboxes of a document is formatted within a rectangular area -- the page box -- that has a finite width and height. The page box does not necessarily correspond toonto the real sheetsheets where the document will ultimately be rendered (paper, transparency, screen, etc.). The CSS page model specifies formatting in the page box, but itThere is the user agent's responsibility to transfer theoften a 1-to-1 relationship between a page box toand a sheet, but this is not always the sheet. Somecase. Transfer possibilities include:

Although CSS2 does not specify how user agents transfer page boxes to sheets, it does include certain mechanisms for telling user agents about the target sheet size and orientation .13.2 Page boxes: the @page rule

The page box is a rectangular region that contains two areas:

The future.size of a page box cannot be specified in CSS 2.1.

Authors can specify the dimensions, orientation, margins, etc.margins of a page box withininside an @page rule. An @page rule consists of the keyword "@page", a page selector (followed with no intervening spacefollowed by an optional page pseudo-class), andselector, followed by a block ofcontaining declarations and at-rules. Comments and white space are allowed, but optional, between the @page token and the page selector and between the page selector and the block. The declarations (saidin an @page rule are said to be in the page context )..

Note: CSS level 2 has no at-rules that may appear inside @page, but such at-rules are expected to be defined in level 3.

The page selector specifies for which pages the declarations apply. In CSS2,CSS 2.1, page selectors may designate the first page, all left pages, or all right pages, or a page with a specific name. The dimensions ofpages

The page boxrules for handling malformed declarations, malformed statements, and invalid at-rules inside @page are setas defined in section 4.2, with the 'size' property. The dimensions offollowing addition: when the page area areUA expects the dimensionsstart of the page box minus the margin area. Example(s): For example, the following @page rule sets the page box sizea declaration or at-rule (i.e., an IDENT token or an ATKEYWORD token) but finds an unexpected token instead, that token is considered to 8.5 x 11 inches and creates '2cm' margin on all sides between the page box edge andbe the page area: @page { size 8.5in 11in; margin: 2cm }first token of a malformed declaration. I.e., the 'marks' property in an @pagerule specifies crop and cross marksfor the page box.malformed declarations, rather than malformed statements is used to determine which tokens to ignore in that case.

13.2.1 Page margins

In CSS 2.1, only the margin properties ('margin-top', 'margin-right', 'margin-bottom', 'margin-left', and 'margin') apply within the page context. The following diagram shows the relationships between the sheet, page box, and page margins:

Illustration of sheet, page
box, margin, and page area.   [D]

The computed value of box margins at the top or bottom of the page areaExample(s):

Here is '0'.a simple example which sets all page margins on all pages:

@page {
  margin: 3cm;
}

The page context has no notion of fonts, so 'em' and 'ex' units are not allowed. Percentage values on the margin properties are relative to the dimensions of the page box; for left and right margins, they refer to page box width while for top and bottom margins, they refer to page box height. All other units associated with the respective CSS2 properties are allowed. Due to negative margin values (either on the page box or on elements) or absolute positioning content may end up outside the page box, but this content may be "cut" -- by the user agent, the printer, or ultimately, the paper cutter. 13.2.2 Page size : the 'size' property 'size' Value:    <length> {1,2} | auto | portrait | landscape | inherit Initial:    auto Applies to:    the page context Inherited:    N/A Percentages:    N/A Media:    visual , paged This property specifies the size and orientation of a page box. The size of a page box may either be "absolute" (fixed size) or "relative" (scalable, i.e., fitting available sheet sizes). Relative page boxes allow user agents to scale a document and make optimal use of the target size. Three values for the 'size' property create a relative page box: auto The page box will be set to the size and orientation of the target sheet. landscape Overrides the target's orientation. The page box is the same size as the target, and the longer sides are horizontal. portrait Overrides the target's orientation. The page box is the same size as the target, and the shorter sides are horizontal. Example(s): In the following example, the outer edges of the page box will align with the target. The percentage value on the 'margin' property is relative to the target size so if the target sheet dimensions are 21.0cm x 29.7cm (i.e., A4), the margins are 2.10cm and 2.97cm. @page { size: auto; /* auto is the initial value */ margin: 10%; } Length values for the 'size' property create an absolute page box. If only one length value is specified, it sets both the width and height of the page box (i.e., the box is a square). Since the page box is the initial containing block , percentage values are not allowed for the 'size' property. Example(s): For example: @page { size: 8.5in 11in; /* width height */ } The above example set the width of the page box to be 8.5in and the height to be 11in. The page box in this example requires a target sheet size of 8.5"x11" or larger. User agents may allow users to control the transfer of the page box to the sheet (e.g., rotating an absolute page box that's being printed). Rendering page boxes that do not fit a target sheet If a page box does not fit the target sheet dimensions, the user agent may choose to: Rotate the page box 90° if this will make the page box fit. Scale the page to fit the target. The user agent should consult the user before performing these operations. Positioning the page box on the sheet When the page box is smaller than the target size, the user agent is free to place the page box anywhere on the sheet. However, it is recommended that the page box be centered on the sheet since this will align double-sided pages and avoid accidental loss of information that is printed near the edge of the sheet. 13.2.3 Crop marks : the 'marks' property 'marks' Value:    [ crop || cross ] | none | inherit Initial:    none Applies to:    page context Inherited:    N/A Percentages:    N/A Media:    visual , paged In high-quality printing, marks are often added outsideright margins, they refer to the page box. This property specifies whether cross marks or crop marks or both should be rendered just outsidewidth of the page box edge. Crop marks indicate wherewhile for top and bottom margins, they refer to the height of the page should be cut. Cross marks (also known as register marks or registration marks)box. All other units associated with the respective CSS 2.1 properties are usedallowed.

Due to align sheets. Marks are visible onlynegative margin values (either on absolute page boxes (see the 'size' property). In relative page boxes,the page box will be aligned with the target andor on elements) or absolute positioning content may end up outside the marks willpage box, but this content may be outside"cut" — by the printable area.user agent, the size, style, and position of cross marks depend onprinter, or ultimately, the user agent. 13.2.4paper cutter.

13.2.2 Page selectors: selecting left, right, and first pages

When printing double-sided documents, the page boxes on left and right pages shouldmay be different. This can be expressed through two CSS pseudo-classes that may be definedused in thepage context .selectors.

All pages are automatically classified by user agents into either the :left or :right pseudo-class. Whether the first page of a document is :left or :right depends on the major writing direction of the root element. For example, the first page of a document with a left-to-right major writing direction would be a :right page, and the first page of a document with a right-to-left major writing direction would be a :left page. To explicitly force a document to begin printing on a left or right page, authors can insert a page break before the first generated box.

Example(s):

@page :left {
  margin-left: 4cm;
  margin-right: 3cm;
}

@page :right {
  margin-left: 3cm;
  margin-right: 4cm;
}

If different declarations have been given for left and right pages, the user agent must honor these declarations even if the user agent does not transfer the page boxes to left and right sheets (e.g., a printer that only prints single-sided).

Authors may also specify style for the first page of a document with the :first pseudo-class:

Example(s):

@page { margin: 2cm } /* All margins set to 2cm */

@page :first {
  margin-top: 10cm    /* Top margin on first page 10cm */
}

Whether the first page of a document is :left or :right depends on the major writing direction of the document and is outside the scope of this document. However, to force a :left or :right first page, authors may insert a page break before the first generated box (e.g., in HTML, specify this for the BODY element).Properties specified in a :left (or :right)or :right @page rule override those specified in an @page rule that has no pseudo-class specified. Properties specified in a :first @page rule override those specified in :left (or :right)or :right @page rules.

Note. Adding declarations toIf a forced break occurs before the :left or :right pseudo-class does not influencefirst generated box, it is undefined in CSS 2.1 whether ':first' applies to the document comes out ofblank page before the printer double-break or single-sided (which is outsideto the scope ofpage after it.

Margin declarations on left, right, and first pages may result in different page area widths. To simplify implementations, user agents may use a single page area width on left, right, and first pages. In this specification). Note. Future versionscase, the page area width of CSS may include otherthe first page pseudo-classes. 13.2.5should be used.

13.2.3 Content outside the page box

When formatting content in the page model, some content may end up outside the current page box. For example, an element whose 'white-space' property has the value 'pre' may generate a box that is wider than the page box. Also,As another example, when boxes are positioned absolutely ,or relatively, they may end up in "inconvenient" locations. For example, images may be placed on the edge of the page box or 100,000 inchesmeters below the page box.

A specification forThe exact formatting of such elements lies outside the scope of this document.specification. However, we recommend that authors and user agents observe the following general principles concerning content outside the page box:

13.3 Page breaks

The following sections explainThis section describes page formattingbreaks in CSS2.CSS 2.1. Five properties indicate where the user agent may or should break pages, and on what page (left or right) the subsequent content should resume. Each page break ends layout in the current page box and causes remaining pieces of the document tree to be laid out in a new page box.

13.3.1 Page break before/after elementsproperties: 'page-break-before', 'page-break-after', 'page-break-inside'

'page-break-before'
Value:  auto | always | avoid | left | right | inherit
Initial:  auto
Applies to:  block-level elements (but see text)
Inherited:  no
Percentages:  N/A
Media:  visual, paged
Computed value:  as specified
'page-break-after'
Value:  auto | always | avoid | left | right | inherit
Initial:  auto
Applies to:  block-level elements (but see text)
Inherited:  no
Percentages:  N/A
Media:  visual, paged
Computed value:  as specified
'page-break-inside'
Value:  avoid | auto | inherit
Initial:  auto
Applies to:  block-level elements (but see text)
Inherited:   yesno
Percentages:  N/A
Media:  visual, paged
Computed value:  as specified

Values for these properties have the following meanings:

auto
Neither force nor forbid a page break before (after, inside) the generated box.
always
Always force a page break before (after) the generated box.
avoid
Avoid a page break before (after, inside) the generated box.
left
Force one or two page breaks before (after) the generated box so that the next page is formatted as a left page.
right
Force one or two page breaks before (after) the generated box so that the next page is formatted as a right page.

A conforming user agent may interpret the values 'left' and 'right' as 'always'.

A potential page break location is typically under the influence of the parent element's 'page-break-inside' property, the 'page-break-after' property of the preceding element, and the 'page-break-before' property of the following element. When these properties have values other than 'auto', the values 'always', 'left', and 'right' take precedence over 'avoid'.

See the section on allowed page breaks for the exact rules on how these properties may force or suppress a page break. 13.3.2 Using named pages: 'page' 'page' Value:    <identifier> | auto Initial:    auto Applies to:    block-level elements Inherited:    yes Percentages:    N/A Media:    visual , paged The 'page' property can be used to specify a particular type of page where an element should be displayed. Example(s): This example will put all tables on a right-hand side landscape page (named "rotated"): @page rotated {size: landscape} TABLE {page: rotated; page-break-before: right} The 'page' property works as follows: If a block box with inline content has a 'page' property that is different from the preceding block box with inline content, then one or two page breaks are inserted between them, and the boxes afterUser Agents must apply these properties to block-level elements in the break are rendered on a page boxnormal flow of the named type. See "Forcedroot element. User agents may also apply these properties to other elements, e.g., 'table-row' elements.

When a page breaks" below. Example(s): In this example, the two tables are rendered on landscape pages (indeed, onbreak splits a box, the same page, if they fit),box's margins, borders, and padding have no visual effect where the page type "narrow" is not used at all, despite having been set on the DIV: @page narrow {size: 9cm 18cm} @page rotated {size: landscape} DIV {page: narrow} TABLE {page: rotated} with this document:: <DIV> <TABLE>...</TABLE> <TABLE>...</TABLE> </DIV> 13.3.3split occurs.

13.3.2 Breaks inside elements: 'orphans', 'widows'

'orphans'
Value:  <integer> | inherit
Initial:  2
Applies to:   block-levelblock container elements
Inherited:  yes
Percentages:  N/A
Media:  visual, paged
Computed value:  as specified
'widows'
Value:  <integer> | inherit
Initial:  2
Applies to:   block-levelblock container elements
Inherited:  yes
Percentages:  N/A
Media:  visual, paged
Computed value:  as specified

The 'orphans' property specifies the minimum number of lines ofin a paragraphblock container that must be left at the bottom of a page. The 'widows' property specifies the minimum number of lines ofin a paragraphblock container that must be left at the top of a page. Examples of how they are used to control page breaks are given below.

Only positive values are allowed.

For information about paragraph formatting, please consult the section on line boxes.

13.3.413.3.3 Allowed page breaks

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

  1. In the vertical margin between blockblock-level boxes. When aan unforced page break occurs here, the computedused 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.
  2. Between line boxes inside a block container box.
  3. Between the content edge of a block container box and the outer edges of its child content (margin edges of block-level children or line box edges for inline-level children) if there is a (non-zero) gap between them.

Note: It is expected that CSS3 will specify that the relevant 'margin-top' applies (i.e., is not set to '0') after a forced page break.

These breaks are subject to the following rules:

If the above doesn'tdoes not provide enough break points to keep content from overflowing the page boxes, then rules A, B and D are dropped in order to find additional breakpoints.

If that still does not lead to sufficient break points, rules A andrule C areis dropped as well, to find still more break points.

Page breaks cannot occur inside boxes that are absolutely positioned . 13.3.513.3.4 Forced page breaks

A page break must occur at (1) if, among the 'page-break-after' and 'page-break-before' properties of all the elements generating boxes that meet at this margin, there is at least one with the value 'always', 'left', or 'right'.

A page break must also occur at (1) if the last line box above this margin and the first one below it do not have the same value for 'page' . 13.3.613.3.5 "Best" page breaks

CSS2CSS 2.1 does not define which of a set of allowed page breaks must be used; CSS2CSS 2.1 does not forbid a user agent from breaking at every possible break point, or not to break at all. But CSS2CSS 2.1 does recommend that user agents observe the following heuristics (while recognizing that they are sometimes contradictory):

Example(s):

Suppose, for example, that the style sheet contains 'orphans :'orphans: 4', 'widows :'widows: 2', and there are 20 lines (line boxes) available at the bottom of the current page:

Now suppose that 'orphans' is '10', 'widows' is '20', and there are 8 lines available at the bottom of the current page:

13.4 Cascading in the page context

Declarations in the page context obey the cascade just like normal CSS2CSS declarations.

Example(s):

Consider the following example:

@page {
  margin-left: 3cm;
}

@page :left {
  margin-left: 4cm;
}

Due to the higher specificity of the pseudo-class selector, the left margin on left pages will be '4cm' and all other pages (i.e., the right pages) will have a left margin of '3cm'.