Background Images

You can add a background image to all content pages in your document by setting the page-background-image document attribute. You can also set the background images for recto and verso pages separately using the page-background-image-recto and page-background-image-verso document attributes. You can set (or override) the background image applied to the title page using the title-page-background-image document attribute. If you want to overlay a background image on top of the page content (i.e., a watermark), you can do so using the page-foreground-image.

This page explains how these document attributes are used. It also covers how attributes on the image macro can be used to control the size, fit, and position of the image. All the document attributes covered on this page must be defined in the document header.

As an alternative to using document attributes, you can define these background images and the foreground image in your theme.

page-background-image attribute

Background images can be applied to all content pages using the page-background-image document attribute. A content page is defined as every page in the document except for cover and imported pages. The list of content pages includes the title page as well as non-facing pages in a prepress document.

As a counterpart, a foreground image can be added to all pages using the page-foreground-image. Unlike the background image, the foreground image cannot be added per side or page layout.

The page-background-image attribute accepts an inline image macro. The target of the image macro can be an absolute path or a path relative to imagesdir.

= Document Title
:page-background-image: image:bg.png[]

You can also assign a page background image per side or per page layout.

When you set page-background-image, the specified image will be applied to the title page as well. You can specify a different image for the title page using the title-page-background-image document attribute.

= Document Title
:doctype: book
:page-background-image: image:bg.png[]
:title-page-background-image: image:title-bg.png[]

Alternately, you can turn off the background image on the title page by assigning the value none.

= Document Title
:doctype: book
:page-background-image: image:bg.png[]
:title-page-background-image: none

By default, page background images are automatically scaled to fit the bounds of the page (i.e., fit=contain) and centered (i.e., position=center). To alter this behavior, you can set the fit, sizing, and position attributes on the image macro.

fit attribute

Background images, including front and back cover images, can be sized relative to the page using the fit attribute on the image macro. The fit attribute works similarly to the object-fit property in CSS. Its value must be specified as a single keyword, chosen from the table below. The starting size of the image is determined by the explicit width, if specified, or the implicit width. The height is always derived from the width while respecting the implicit aspect ratio of the image. The available space—​the canvas—​for a background image is the page. If the fit attribute is not specified, it defaults to contain. The contain value automatically scales the image to fit the bounds of the page.

Values accepted by the fit attribute
Value Purpose

contain

The image is scaled up or down while retaining its aspect ratio to fit within the available space. (default)

cover

The image is scaled up or down while retaining its aspect ratio so the image completely covers the available space, even if it means the image must be clipped in one direction.

scale-down

The image is scaled down while retaining its aspect ratio to fit within the available space. If the image already fits, it is not scaled.

fill

The image is scaled to fit the available space even if it requires modifying the aspect ratio of the image. The fill value does not apply to SVG images.

none

The image is not scaled.

If you want to scale the image using one of the sizing attributes (pdfwidth, scaledwidth, width) on the image macro, you must set the fit attribute to the value none (i.e., fit=none).

= Document Title
:page-background-image: image:bg.png[fit=none,pdfwidth=50%] (1)
1 fit must be assigned the value none when using pdfwidth or any of the other sizing attributes.

The fit attribute is often combined with the position attribute, covered next, to control the placement of the image on the canvas.

position attribute

In addition to scaling, background images for cover pages, content pages, and the title page support positioning using the position attribute on the image macro. The position attribute accepts a syntax similar to the background-position property in CSS, except only the keyword positions are supported. The position consists of two values, the vertical position and the horizontal position (e.g., top center). If only one value is specified (e.g., top), the other value is assumed to be center. If the position attribute is not specified, its value defaults to is center center. The center center value centers the image vertically and horizontally on the page.

The following table lists of the vertical and horizontal positioning keywords the position attribute supports. You can use any combination of these keywords to position the image.

Vertical Positions Horizontal Positions

top
center
bottom

left
center
right

Here’s an example of how to place a background image at the top center of a page:

= Document Title
:page-background-image: image:bg.png[position=top]

Here’s how to move it to the bottom right:

= Document Title
:page-background-image: image:bg.png[fit=none,pdfwidth=50%,position=bottom right]

If an image dimension matches the height or width of the page, the positioning keyword for that axis has no effect.

Recto and verso background images

If a background image is assigned to the recto pages (right-hand, odd-numbered pages) or verso pages (left-hand, even-numbered pages), the background will be used only for that side.

= Document Title
:page-background-image-recto: image:bg.png[fit=none,pdfwidth=20%,position=bottom right]
:page-background-image-verso: image:bg.png[fit=none,pdfwidth=20%,position=bottom left]

If a background image isn’t specified for a side, the converter will use the default page background image (page-background-image), if specified. To turn off the background image for a side, assign the value none.

Per page layout

You can configure any background image to be selected per page layout by including the escaped {page-layout} attribute reference in the image path. The reference has to be escaped so it’s not expanded until the converter resolves the image for a given page layout.

= Document Title
:page-background-image: image:bg-\{page-layout}.png[]

The image is resolved the first time a given page layout is used in the document. Using this technique requires that the page layout value be included in the filename of the image (e.g., footer-bg-landscape.png).