files/en-us/web/css/reference/at-rules/@page/index.md
The @page at-rule is a CSS at-rule used to modify different aspects of printed pages. It targets and modifies the page's dimensions, orientation, and margins. The @page at-rule can be used to target all pages in a print-out or a subset using its various pseudo-classes.
/* Targets all the pages */
@page {
size: 8.5in 9in;
margin-top: 4in;
}
/* Targets all even-numbered pages */
@page :left {
margin-top: 4in;
}
/* Targets all odd-numbered pages */
@page :right {
size: 11in;
margin-top: 4in;
}
/* Targets all selectors with `page: wide;` set */
@page wide {
size: a4 landscape;
}
@page {
/* margin box at top right showing page number */
@top-right {
content: "Page " counter(pageNumber);
}
}
The @page at-rule can contain only page descriptors and margin at-rules. The following descriptors have been implemented by at least one browser:
page-orientation
size
The specification mentions following CSS properties to be applicable to page boxes via @page at-rule. But these have not been supported by any user agent yet.
<details> <summary>Remaining page properties</summary>| Feature | CSS properties |
|---|---|
| bidi properties | direction |
| background properties | background-color |
| background-image | |
| background-repeat | |
| background-attachment | |
| background-position | |
| background | |
| border properties | border-top-width |
| border-right-width | |
| border-bottom-width | |
| border-left-width | |
| border-width | |
| border-top-color | |
| border-right-color | |
| border-bottom-color | |
| border-left-color | |
| border-color | |
| border-top-style | |
| border-right-style | |
| border-bottom-style | |
| border-left-style | |
| border-short-style | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| border | |
| counter properties | counter-reset |
| counter-increment | |
| color | color |
| font properties | font-family |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| font | |
| height properties | height |
| min-height | |
| max-height | |
| line height | line-height |
| margin properties | margin-top |
| margin-right | |
| margin-bottom | |
| margin-left | |
| margin | |
| outline properties | outline-width |
| outline-style | |
| outline-color | |
| outline | |
| padding properties | padding-top |
| padding-right | |
| padding-bottom | |
| padding-left | |
| padding | |
| quotes | quotes |
| text properties | letter-spacing |
| text-align | |
| text-decoration | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| visibility | visibility |
| width properties | width |
| min-width | |
| max-width |
The @page rule defines properties of the page box. The @page at-rule can be accessed via the CSS object model interface {{domxref("CSSPageRule")}}.
[!NOTE] The W3C is discussing how to handle viewport-related {{cssxref("<length>")}} units,
vh,vw,vmin, andvmax. Meanwhile do not use them within a@pageat-rule.
The @page at-rule, allows the user to assign a name to the rule, which is then called in a declaration using the page property.
{{csssyntax}}
Where the <page-body> includes:
and <pseudo-page> represents these pseudo-classes:
The margin at-rules are used inside of the @page at-rule. They each target a different section of the document printed page, styling the area of the printed page based on the property values set in the style block:
@page {
@top-left {
/* page-margin-properties */
}
}
@top-left targets the top-left of the document and applies the changes based on the page-margin-properties set.
Other margin-at rules include:
@top-left-corner
@top-left
@top-center
@top-right
@top-right-corner
@bottom-left-corner
@bottom-left
@bottom-center
@bottom-right
@bottom-right-corner
@left-top
@left-middle
@left-bottom
@right-top
@right-middle
@right-bottom
The page-margin properties are the set of CSS properties can be set in any individual margin at-rule. They include:
<details> <summary>Page-margin properties</summary>| Feature | CSS properties |
|---|---|
| bidi properties | direction |
| background properties | background-color |
| background-image | |
| background-repeat | |
| background-attachment | |
| background-position | |
| background | |
| border properties | border-top-width |
| border-right-width | |
| border-bottom-width | |
| border-left-width | |
| border-width | |
| border-top-color | |
| border-right-color | |
| border-bottom-color | |
| border-left-color | |
| border-color | |
| border-top-style | |
| border-right-style | |
| border-bottom-style | |
| border-left-style | |
| border-short-style | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| border | |
| counter properties | counter-reset |
| counter-increment | |
| content | content |
| color | color |
| font properties | font-family |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| font | |
| height properties | height |
| min-height | |
| max-height | |
| line height | line-height |
| margin properties | margin-top |
| margin-right | |
| margin-bottom | |
| margin-left | |
| margin | |
| outline properties | outline-width |
| outline-style | |
| outline-color | |
| outline | |
| padding properties | padding-top |
| padding-right | |
| padding-bottom | |
| padding-left | |
| padding | |
| quotes | quotes |
| text properties | letter-spacing |
| text-align | |
| text-decoration | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| vertical alignment | vertical-align |
| visibility | visibility |
| width properties | width |
| min-width | |
| max-width | |
| z-index | z-index |
Named pages enable performing per-page layout and adding page-breaks in a declarative manner when printing.
Named pages can be applied using the {{Cssxref("page")}} property. This allows the user to create different page configurations for use in print layouts.
An example of this can be found on the page examples.
This example shows how to split the <section>s into individual pages in landscape format with each page having a 20% margin when printed.
Clicking the print button will launch a print dialog with the HTML sections split into individual pages.
<button>Print page</button>
<article>
<section>
<h2>Header one</h2>
<p>Paragraph one.</p>
</section>
<section>
<h2>Header two</h2>
<p>Paragraph two.</p>
</section>
<section>
<h2>Header three</h2>
<p>Paragraph three.</p>
</section>
</article>
const button = document.querySelector("button");
button.addEventListener("click", () => {
window.print();
});
@page {
size: landscape;
margin: 2cm;
}
section {
page-break-after: always;
break-after: page;
}
@media print {
button {
display: none;
}
}
body {
font-family: "Helvetica", sans-serif;
background-color: silver;
}
article {
width: 100%;
}
section {
display: grid;
background-color: white;
border-radius: 0.6rem;
justify-items: center;
padding: 1rem;
width: 50%;
print-color-adjust: exact;
-webkit-print-color-adjust: exact;
margin: 0 auto;
margin-block-end: 1rem;
border: 1px dashed;
}
{{EmbedLiveSample('page-size', '100%', '540', , , , , "allow-modals")}}
See the various pseudo-classes of @page for examples.
{{Specifications}}
{{Compat}}
@page size descriptor