`page` CSS property
The page CSS property is used to specify the named page, a specific type of page defined by the @page at-rule.
The page CSS property is used to specify the named page, a specific type of page defined by the @page at-rule.
If there are multiple selectors that are using a named page consecutively then a forced page break using break-after may be needed.
Syntax
/* set a named page */
page: exampleName;
page: chapterIntro;
/* Use ancestors named page */
page: auto; /* default value */
/* Global values */
page: inherit;
page: initial;
page: revert;
page: revert-layer;
page: unset;
Values
auto- : Default value. Use the value of the nearest ancestor with a non-
autovalue. If no ancestor has a named page value set, the used value for auto is the empty string. - custom-ident
- : Case-sensitive name defined in a @page at-rule.
Formal definition
Formal syntax
Examples
Named page example
In this example, there are two parts to this HTML; print controls and the content to be printed.
The print controls allow the user to select how the sections in the article will be printed.
```html live-sample___page-property
Table of contents
- Foreword
- Introduction
- Chapter One - named pages
- Chapter Two - page orientation
- Chapter Three - page margins
- Conclusion
Foreword
This book is all about how the CSS @page at-rule can help
with printing HTML books.
Introduction
This book is a concept to show how an HTML document can easily be printed out in pages.
Named pages
Lorem ipsum
Page Orientation
Lorem ipsum
Page Margins
There are 16 page margins that can be set:
- @top-left-corner
- @top-left
- @top-center
- @top-right
- @top-right-corner
- @left-top
- @left-middle
- @left-bottom
- @right-top
- @right-middle
- @right-bottom
- @bottom-left-corner
- @bottom-left
- @bottom-center
- @bottom-right
- @bottom-right-corner
They can be used to show what appears in these parts of the margin
Conclusion
Now go ahead and write books.
The first part of the CSS sets up the **named** pages, these include the size and orientation and also some content to go in the [`@top-center` margin](/docs/Web/CSS/Reference/At-rules/@page#margin_at-rules) of the printed pages.
```css live-sample___page-property
@page toc {
size: a4 portrait;
@top-center {
content: "Table of contents";
}
}
@page foreword {
size: a4 portrait;
@top-center {
content: "Foreword";
}
}
@page introduction {
size: a4 portrait;
@top-center {
content: "Introduction";
}
}
@page conclusion {
size: a4 portrait;
@top-center {
content: "Conclusion";
}
}
@page chapter {
size: a4 landscape;
@top-center {
content: "Chapter";
}
}
```css hidden live-sample___page-property fieldset { display: flex; flex-direction: row; justify-content: space-between; gap: 1rem; width: fit-content; } body { font: 1.1em sans-serif; }
The next part of the CSS uses [attribute selectors](/docs/Web/CSS/Reference/Selectors/Attribute_selectors) to apply the print dimensions, orientation, and margins defined in the named `@page` rules defined in the previous CSS section to elements using the `page` property.
The sections with `class="chapter"` are concurrent and appear as one page.
The `break-after: page;` is used to split them up, which splits each chapter into a separately printed page.
```css live-sample___page-property
@media print {
fieldset {
display: none;
}
section {
font-size: 2rem;
font-family: "Roboto", sans-serif;
}
.chapter {
border: tomato 2px solid;
}
[data-print="grouped"] > #toc,
[data-print="paged"] > #toc {
page: toc;
font-family: "Courier New";
}
[data-print="grouped"] > #foreword,
[data-print="paged"] > #foreword {
page: foreword;
font-family: "Courier New";
}
[data-print="grouped"] > #introduction,
[data-print="paged"] > #introduction {
page: introduction;
font-family: "Courier New";
}
[data-print="grouped"] > #conclusion,
[data-print="paged"] > #conclusion {
page: conclusion;
font-family: "Courier New";
}
[data-print="grouped"] > .chapter,
[data-print="paged"] > .chapter {
page: chapter;
}
[data-print="paged"] > .chapter {
border: none;
break-after: page;
}
.chapter > ul {
columns: 2;
}
}
The JavaScript updates the value of the data-print attribute, which is the attribute on which the named page is applied, when you select a different printing option:
```js live-sample___page-property const printArea = document.querySelector("#print-area"); const printButton = document.querySelector("#print"); const printOption = document.querySelector("#printStyle");
printOption.addEventListener("change", (event) => { if (event.target.value === "single") { printArea.dataset.print = "single"; } else if (event.target.value === "grouped") { printArea.dataset.print = "grouped"; } else { printArea.dataset.print = "paged"; } });
printButton.addEventListener("click", () => { window.print(); }); ```
What is printed, and what is shown in the print preview dialog, will change depending on which print style radio button is selected:
100%